SIGHAX HYPE! Tried getting interrupts working

This commit is contained in:
Morten Delenk 2017-05-20 20:04:22 +00:00
parent 0dae0b1cd4
commit 3394c1d9bc
14 changed files with 146 additions and 22 deletions

View file

@ -1,9 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys import sys
import base64
if len(sys.argv) < 4: if len(sys.argv) < 4:
print("USAGE: mkfirm.py <kernel9.elf> <kernel11.elf> <output.firm>") print("USAGE: mkfirm.py <kernel9.elf> <kernel11.elf> <output.firm>")
print("WARNING: Currently you cannot install this FIRM, because YOU'LL BRICK YOUR 3DS!") sighax_sig=base64.a85decode(b'DZ:iLO%T*f"sal5!m5:QU$Gmm[dg)m,&2*<U<LK%FP[g"\'Kc#M==?kH6r/uA>-HJLBEU+9e;EeCligEb]Qpn^2!Gp5_g%-,]IDC-MMahK=@9T\\,0,4&6MDgNaP\'t06CY.qpSb;KMn)+PR1>e0IbaRdgDC5J-m3DLB2d$%DNZn-W7=ADNLl+s&Z2gs%A?=ZJ:91F?66n9DXZ.-lL+5$#ebaKH5IB)Y&DTGd%!,O4me2`$F["\\.$Tr)%\\,h,n.Q]LTQGQRH:#a?1l(T-i_m7b\\7Mg5Ga7LDUacTc`!af0V`"\'-*]I]3%SL)@)ll701ok)i')
sighax_sig = b'\x00'*256 #TODO insert sighax signature here.
import struct import struct
import hashlib import hashlib
def get_elf_seg(f): #Return entry,section_beg,section_size,section def get_elf_seg(f): #Return entry,section_beg,section_size,section
@ -49,6 +49,18 @@ f2 = open(sys.argv[2],"rb")
f = open(sys.argv[3],"wb") f = open(sys.argv[3],"wb")
arm9_entry,arm9_section_beg,arm9_section_size,arm9_section = get_elf_seg(f1) arm9_entry,arm9_section_beg,arm9_section_size,arm9_section = get_elf_seg(f1)
arm11_entry,arm11_section_beg,arm11_section_size,arm11_section = get_elf_seg(f2) arm11_entry,arm11_section_beg,arm11_section_size,arm11_section = get_elf_seg(f2)
def align(a,b):
if a % b:
return a + b - (a%b)
return a
def align2(a,b):
if len(a) % b:
return a + bytes(512-(len(a)%b))
return a
arm9_section_size=align(arm9_section_size,512)
arm11_section_size=align(arm11_section_size,512)
arm9_section=align2(arm9_section,512)
arm11_section=align2(arm11_section,512)
arm9_hash=hashlib.sha256(arm9_section).digest() arm9_hash=hashlib.sha256(arm9_section).digest()
arm11_hash=hashlib.sha256(arm11_section).digest() arm11_hash=hashlib.sha256(arm11_section).digest()
arm9_off = 0x200 arm9_off = 0x200

View file

@ -1,5 +1,5 @@
builddir() { builddir() {
rm -rvvf build/ && rm -rf build/ &&
mkdir -pv build/ mkdir -pv build/
} }
rm -rvvf out/ rm -rvvf out/
@ -59,4 +59,15 @@ mv kernel9 build/kernel &&
buildtools/sighax-firm.sh && buildtools/sighax-firm.sh &&
mv sighax.firm out/ && mv sighax.firm out/ &&
cp -v build/kernel/kernel out/arm11loaderhax.elf cp -v build/kernel/kernel out/arm11loaderhax.elf
rm -rvvf build/ rm -rf build/
#firmloader
pushd boot/3ds &&
builddir &&
pushd build &&
cmake -DCMAKE_TOOLCHAIN_FILE=../../../toolchains/arm-none-eabi.cmake .. &&
make -j$(nproc) &&
popd &&
mv build/arm9loaderhax.bin ../../out &&
rm -rf build/ &&
popd

View file

@ -1,4 +1,5 @@
#include "../../../hw/3ds11/picafb/picafb.hpp" #include "../../../hw/3ds11/picafb/picafb.hpp"
#include "../../../hw/3ds11/vectorinit/vectorinit.hpp"
#include <base.hpp> #include <base.hpp>
#include <config.h> #include <config.h>
@ -8,4 +9,6 @@ extern "C" void start() { main(); }
void drivers_init() { void drivers_init() {
setMainTTY(&term); setMainTTY(&term);
--term; --term;
initVectors();
asm volatile("svc #0");
} }

View file

@ -8,6 +8,28 @@
_start: _start:
CPSID aif //Disable interrupts CPSID aif //Disable interrupts
ldr sp, =kernel_stack ldr sp, =kernel_stack
//set other stacks
mrs r0, cpsr
bic r2, r0, #0x1F
mov r1, r2
orr r1, #0b10001 //FIQ
msr cpsr, r1
ldr sp, =interrupt_stack
mov r1, r2
orr r1, #0b10010 //IRQ
msr cpsr, r1
ldr sp, =interrupt_stack
mov r1, r2
orr r1, #0b10111 //Abort
msr cpsr, r1
ldr sp, =exception_stack
mov r1, r2
orr r1, #0b11011 //Undefined
msr cpsr, r1
ldr sp, =exception_stack
orr r1, #0b11111 //SYS
msr cpsr, r1
ldr sp, =kernel_stack
//Enable FPU //Enable FPU
mov r0, #0 mov r0, #0
mov r1, #0xF00000 mov r1, #0xF00000
@ -17,9 +39,14 @@ _start:
mov r2, #0x3C00000 mov r2, #0x3C00000
fmxr fpexc, r1 fmxr fpexc, r1
fmxr fpscr, r2 fmxr fpscr, r2
//Start MTGos //Start MTGos
blx start blx start
.section .bss .section .bss
.space 4096
interrupt_stack:
.space 4096
exception_stack:
.space 16384 .space 16384
kernel_stack: kernel_stack:

View file

@ -1,3 +1,3 @@
SET(PLATFORM_C_FLAGS "-I../../kernel/arch/arm/3ds9/include -mcpu=arm946e-s -march=armv5te -mthumb-interwork -mthumb -Os") SET(PLATFORM_C_FLAGS "-I../../kernel/arch/arm/3ds9/include -mcpu=arm946e-s -march=armv5te -mthumb-interwork -marm -O9")
SET(PLATFORM_CXX_FLAGS "${PLATFORM_C_FLAGS}") SET(PLATFORM_CXX_FLAGS "${PLATFORM_C_FLAGS}")
SET(PLATFORM_ASM_FLAGS "${PLATFORM_C_FLAGS}") SET(PLATFORM_ASM_FLAGS "${PLATFORM_C_FLAGS}")

View file

@ -28,6 +28,32 @@ _start:
blx r0 blx r0
*/ */
ldr sp, =kernel_stack //set stack ldr sp, =kernel_stack //set stack
//Set other stacks
mrs r0, cpsr
bic r2, r0, #0x1F
mov r1, r2
orr r1, #0b10001 //FIQ
msr cpsr, r1
ldr sp, =interrupt_stack
mov r1, r2
orr r1, #0b10010 //IRQ
msr cpsr, r1
ldr sp, =interrupt_stack
mov r1, r2
orr r1, #0b10111 //Abort
msr cpsr, r1
ldr sp, =exception_stack
mov r1, r2
orr r1, #0b11011 //Undefined
msr cpsr, r1
ldr sp, =exception_stack
mov r1, r2
orr r1, #0b10011 //SVC
msr cpsr, r1
ldr sp, =kernel_stack
orr r1, #0b11111 //SYS
msr cpsr, r1
ldr sp, =kernel_stack
/* /*
//Configure ITCM tosomething //Configure ITCM tosomething
mrc p15, 0, r0, c9, c1, 1 mrc p15, 0, r0, c9, c1, 1
@ -95,5 +121,9 @@ _start:
blx start blx start
.section .bss .section .bss
.space 4096
interrupt_stack:
.space 4096
exception_stack:
.space 16384 .space 16384
kernel_stack: kernel_stack:

24
kernel/arch/arm/cache.s Normal file
View file

@ -0,0 +1,24 @@
.arm
.global drainWriteBuffer
drainWriteBuffer:
mov r0, #0
mcr p15, 0, r0, c7, c10, 4
bx lr
.global flushAll
flushAll:
mov r1, #0 //Segment
1:
mov r0, #0 //Line
2:
orr r2, r1, r0
mcr p15, 0, r2, c7, c14, 2 //Flush current line
add r0, #0x20 //Next line
cmp r0, #0x400
bne 2b
add r1, #0x40000000
cmp r1, #0x0
bne 1b
mov r0, #0
mcr p15, 0, r0, c7, c5, 0 //flush instruction cache
b drainWriteBuffer

View file

@ -0,0 +1,4 @@
extern "C" {
void flushAll();
void drainWriteBuffer();
}

View file

@ -15,6 +15,10 @@ void print_regdump(cpu_state *state) {
(*out << " r11: ").puti(state->r11); (*out << " r11: ").puti(state->r11);
(*out << " r12: ").puti(state->r12); (*out << " r12: ").puti(state->r12);
(*out << " pc: ").puti(state->pc); (*out << " pc: ").puti(state->pc);
(*out << " cpsr: ").puti(state->cpsr);
(*out << " sp: ").puti(state->sp);
(*out << " lr: ").puti(state->lr);
(*out << " returnAddr: ").puti(state->returnAddr);
*out << "\n"; *out << "\n";
} }
extern "C" cpu_state *handleINT(int number, cpu_state *state) { extern "C" cpu_state *handleINT(int number, cpu_state *state) {
@ -25,7 +29,8 @@ extern "C" cpu_state *handleINT(int number, cpu_state *state) {
out->setColor(Color::RED); out->setColor(Color::RED);
print_regdump(state); print_regdump(state);
*out << "KERNEL PANIC: Unhandled CPU exception\n"; *out << "KERNEL PANIC: Unhandled CPU exception\n";
// for(;;); for (;;);
} else {
} }
switch (number) { switch (number) {
case 0: case 0:

View file

@ -5,13 +5,14 @@ int_handler.write(
.section .bss .section .bss
.space 4096 .space 4096
exception_stack: exception_stack:
.section .data
.space 4
oldsp:
.section .text .section .text
.global branch_macro .global branch_macro
branch_macro: branch_macro:
ldr pc, [pc, #-4] //Load the next word into PC ldr pc, [pc, #-4] //Load the next word into PC
.macro interrupt_handler intid .macro interrupt_handler intid
//Set to the correct stack
ldr sp, =exception_stack
push {r0-r12,lr} //Push registers push {r0-r12,lr} //Push registers
//Get previous sp and lr //Get previous sp and lr
@ -41,7 +42,8 @@ branch_macro:
ldr r0, =\intid ldr r0, =\intid
//Jump to generic handler //Jump to generic handler
blx intr_common_handler
bl intr_common_handler
//pop the special registers //pop the special registers
pop {r0, r3, r4, lr} pop {r0, r3, r4, lr}
@ -50,6 +52,7 @@ branch_macro:
orr lr, lr, #1 //Enable thumb mode on return# orr lr, lr, #1 //Enable thumb mode on return#
2: 2:
str lr, [sp, #0x34] //Set correct lr str lr, [sp, #0x34] //Set correct lr
ldmfd sp!, {r0-r12, pc}^ //Return back to original mode ldmfd sp!, {r0-r12, pc}^ //Return back to original mode
.endm .endm
.global data_abort .global data_abort
@ -81,8 +84,8 @@ if ("ENABLE_HARD" in config) and config["ENABLE_HARD"]:
for i,j in enumerate(["fpsid","fpscr","fpexc"]): for i,j in enumerate(["fpsid","fpscr","fpexc"]):
push_regs.append("fmrx r"+str(i+1)+", "+j) push_regs.append("fmrx r"+str(i+1)+", "+j)
pop_regs.append("fmxr "+j+", r"+str(i+1)) pop_regs.append("fmxr "+j+", r"+str(i+1))
push_regs.append("push {r1,r2,r3,r4,r5}") push_regs.append("push {r1,r2,r3}")
pop_regs.append("pop {r1,r2,r3,r4,r5}") pop_regs.append("pop {r1,r2,r3}")
push_regs.append("vpush {d0-d15}") push_regs.append("vpush {d0-d15}")
pop_regs.append("vpop {d0-d15}") pop_regs.append("vpop {d0-d15}")
push_regs.append("mov r1, sp") push_regs.append("mov r1, sp")
@ -102,10 +105,10 @@ int_handler.write(" b panic\n")
reg_struct.write("#include <stdint.h>\nstruct cpu_state {\n") reg_struct.write("#include <stdint.h>\nstruct cpu_state {\n")
if ("ENABLE_HARD" in config) and config["ENABLE_HARD"]: if ("ENABLE_HARD" in config) and config["ENABLE_HARD"]:
for reg in ["fpsid","fpscr","fpexc"]:
reg_struct.write(" uint32_t "+reg+";\n")
for reg in ("d"+str(i) for i in range(16)): for reg in ("d"+str(i) for i in range(16)):
reg_struct.write(" double "+reg+";\n") reg_struct.write(" double "+reg+";\n")
for reg in ["fpsid","fpscr","fpexc"]:
reg_struct.write(" uint32_t "+reg+";\n")
for reg in ["cpsr","sp","lr","returnAddr"]: for reg in ["cpsr","sp","lr","returnAddr"]:
reg_struct.write(" uint32_t "+reg+";\n") reg_struct.write(" uint32_t "+reg+";\n")
for i in range(13): for i in range(13):

View file

@ -8,7 +8,7 @@
#define GL_RGB565_OES 2 #define GL_RGB565_OES 2
#define GL_RGB5_A1_OES 3 #define GL_RGB5_A1_OES 3
#define GL_RGBA4_OES 4 #define GL_RGBA4_OES 4
PICAfb::PICAfb() : Framebuffer(25, 30) { PICAfb::PICAfb() : Framebuffer(50, 15) {
#ifdef ENABLE_SCREENINIT #ifdef ENABLE_SCREENINIT
MCU::enableTopLCD(); MCU::enableTopLCD();
MCU::enableBottomLCD(); MCU::enableBottomLCD();
@ -93,8 +93,8 @@ PICAfb::PICAfb() : Framebuffer(25, 30) {
PICAfb::~PICAfb() {} PICAfb::~PICAfb() {}
auto PICAfb::plotPixel(int x, int y, int col) -> void { auto PICAfb::plotPixel(int x, int y, int col) -> void {
unsigned char *lfb = (unsigned char *)0x18300000; unsigned char *lfb = (unsigned char *)0x18300000;
// XXX I know it's rotated. But I need more vertical space than horizonal space. y=240-y-1;
int off = (y * 240 + x) * 3; int off = (x * 240 + y) * 3;
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
lfb[off++] = col; lfb[off++] = col;
col >>= 8; col >>= 8;

View file

@ -1,11 +1,11 @@
#include "picafb.hpp" #include "picafb.hpp"
#include <config.h> #include <config.h>
PICAfb::PICAfb() : Framebuffer(25, 20) {} PICAfb::PICAfb() : Framebuffer(40, 15) {}
PICAfb::~PICAfb() {} PICAfb::~PICAfb() {}
auto PICAfb::plotPixel(int x, int y, int col) -> void { auto PICAfb::plotPixel(int x, int y, int col) -> void {
unsigned char *lfb = (unsigned char *)0x18300000 + 0x46500; unsigned char *lfb = (unsigned char *)0x18300000 + 0x46500;
// XXX I know it's rotated. But I need more vertical space than horizonal space. y=240-y-1;
int off = (y * 240 + x) * 3; int off = (x * 240 + y) * 3;
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
lfb[off++] = col; lfb[off++] = col;
col >>= 8; col >>= 8;

View file

@ -7,9 +7,12 @@ void normal_irq();
void prefetch_abort(); void prefetch_abort();
void svc_call(); void svc_call();
void undefined_op(); void undefined_op();
void flushAll();
} }
#pragma GCC push_options
#pragma GCC optimize("O0")
void initVectors() { void initVectors() {
uintptr_t *vectors = (uintptr_t *)0x0800000; uintptr_t *vectors = (uintptr_t *)0x08000000;
// branch_macro is a ldr pc, [pc,#-4], meaning it reads the following word as PC // branch_macro is a ldr pc, [pc,#-4], meaning it reads the following word as PC
vectors[0] = branch_macro; vectors[0] = branch_macro;
vectors[1] = (uintptr_t)&normal_irq; vectors[1] = (uintptr_t)&normal_irq;
@ -23,4 +26,6 @@ void initVectors() {
vectors[9] = (uintptr_t)&prefetch_abort; vectors[9] = (uintptr_t)&prefetch_abort;
vectors[10] = branch_macro; vectors[10] = branch_macro;
vectors[11] = (uintptr_t)&data_abort; vectors[11] = (uintptr_t)&data_abort;
flushAll();
} }
#pragma GCC pop_options

View file

@ -16,7 +16,7 @@ auto TTY::setColor(unsigned int c) -> void {
auto TTY::putChar(int c) -> void { auto TTY::putChar(int c) -> void {
auto scroll = [this]() -> void { auto scroll = [this]() -> void {
for (int x = 0; x < this->width; x++) for (int x = 0; x < this->width; x++)
for (int y = 0; y < this->height; y++) this->plotChar(x, y, 0); for (int y = 0; y < this->height; y++) this->plotChar(x, y, 0x20);
this->x = this->y = 0; this->x = this->y = 0;
}; };
switch (c) { switch (c) {
@ -28,7 +28,7 @@ auto TTY::putChar(int c) -> void {
default: default:
plotChar(x, y, c); plotChar(x, y, c);
x++; x++;
if (x > width) { if (x >= width) {
y++; y++;
x = 0; x = 0;
} }