From 9d587f9f750253461314757f2f861c5214563120 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Thu, 26 Dec 2019 18:48:38 +0100 Subject: improve linking of interrupts --- interrupt_vector.S | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'interrupt_vector.S') diff --git a/interrupt_vector.S b/interrupt_vector.S index f71330b..d20bf6d 100644 --- a/interrupt_vector.S +++ b/interrupt_vector.S @@ -1,4 +1,4 @@ -.section ".interrupt_vectors.text" +.section ".interrupts.vector" .global abort_handler .local generic_handler @@ -12,6 +12,8 @@ _interrupt_vectors: b generic_handler b generic_handler +.section ".interrupts.text" + generic_handler: b generic_handler abort_handler_caller: -- cgit v1.2.3 From 700f4c412d42c9b9811269045c0e363a0331bba9 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Sat, 28 Dec 2019 21:54:42 +0100 Subject: split kernel into 2 stages; second stage gets copied to 0x0 and runs from there --- Makefile | 29 ++++++++++++++++++----- boot.S | 27 --------------------- demo_functionality.c | 26 ++------------------ interrupt_vector.S | 53 +++++++++++++++++++++++++++++------------ interrupts.c | 55 +++++++++++++++++++++++++++++++++++------- kernel.c | 44 ---------------------------------- kernel_stage1.S | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ kernel_stage1.ld | 27 +++++++++++++++++++++ kernel_stage2.ld | 52 ++++++++++++++++++++++++++++++++++++++++ linker.ld | 47 ------------------------------------ memory.h | 43 ++++++++++++++++++++++----------- setup.c | 41 ++++++++++++++++++++++++++++++++ 12 files changed, 325 insertions(+), 186 deletions(-) delete mode 100644 boot.S delete mode 100644 kernel.c create mode 100644 kernel_stage1.S create mode 100644 kernel_stage1.ld create mode 100644 kernel_stage2.ld delete mode 100644 linker.ld create mode 100644 setup.c (limited to 'interrupt_vector.S') diff --git a/Makefile b/Makefile index 29efa86..68368a5 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,10 @@ -CFLAGS=-mcpu=cortex-a7 -ffreestanding -std=gnu11 -Wall -Wextra -O2 -fPIC -I. +CFLAGS=-mcpu=cortex-a7 -ffreestanding -std=gnu11 -Wall -Wextra -O2 -I. ELFFLAGS=-nostdlib -lgcc ARM_OBJECTS=kernel.o paging.o demo_functionality.o PL0_test.o uart.o loader_stage1.o loader_stage2.o +KERNEL_STAGE2_OBJECTS=setup.o interrupt_vector.o interrupts.o uart.o demo_functionality.o paging.o ramfs_embeddable.o ramfs.o + RAMFS_FILES=PL_0_test.img all : kernel.img @@ -10,20 +12,32 @@ all : kernel.img %.o : %.c arm-none-eabi-gcc $(CFLAGS) -c $^ -o $@ -%.o : %.S - arm-none-eabi-as -mcpu=cortex-a7 $^ -o $@ - %.img : %.elf arm-none-eabi-objcopy $^ -O binary $@ +%.o : %.S + arm-none-eabi-as -mcpu=cortex-a7 $^ -o $@ + %_embeddable.o : %.img arm-none-eabi-objcopy -I binary -O elf32-littlearm -B arm --rename-section .data=.rodata $^ $@ PL_0_test.elf : PL0_test.o uart.o arm-none-eabi-gcc -T PL0_test.ld -o $@ $(ELFFLAGS) $^ -kernel.elf : boot.o kernel.o uart.o demo_functionality.o paging.o interrupt_vector.o interrupts.o ramfs_embeddable.o ramfs.o - arm-none-eabi-gcc -T linker.ld -o $@ $(ELFFLAGS) $^ +kernel_stage1.o : kernel_stage1.S kernel_stage2.img + arm-none-eabi-as -mcpu=cortex-a7 $< -o $@ + +kernel.elf : kernel_stage1.ld kernel_stage1.o + arm-none-eabi-gcc -T $< -o $@ $(ELFFLAGS) kernel_stage1.o + +kernel.img : kernel.elf + arm-none-eabi-objcopy $^ -O binary $@ + +kernel_stage2.elf : kernel_stage2.ld $(KERNEL_STAGE2_OBJECTS) + arm-none-eabi-gcc -T $< -o $@ $(ELFFLAGS) $(KERNEL_STAGE2_OBJECTS) + +#kernel.elf : boot.o kernel.o uart.o demo_functionality.o paging.o interrupt_vector.o interrupts.o ramfs_embeddable.o ramfs.o +# arm-none-eabi-gcc -T linker.ld -o $@ $(ELFFLAGS) $^ loader_stage2.elf : loader_stage2.o uart.o arm-none-eabi-gcc -T loader_stage2_linker.ld -o $@ $(ELFFLAGS) $^ @@ -42,6 +56,9 @@ loader.img : loader.elf qemu-elf : kernel.elf qemu-system-arm -m 256 -M raspi2 -serial stdio -kernel $^ +qemu-img : kernel.img + qemu-system-arm -m 256 -M raspi2 -serial stdio -kernel $^ + qemu-bin : loader.img kernel.img pipe_image ./pipe_image --stdout | qemu-system-arm -m 256 -M raspi2 -serial stdio -kernel $< diff --git a/boot.S b/boot.S deleted file mode 100644 index 593ed11..0000000 --- a/boot.S +++ /dev/null @@ -1,27 +0,0 @@ -// armv7 mode - -// Entry point for the kernel. -// r15 -> should begin execution at 0x8000. -// r0 -> 0x00000000 -// r1 -> 0x00000C42 -// r2 -> 0x00000100 - start of ATAGS -// preserve these registers as argument for kernel_main - -.global _boot // make entry point label global -_boot: - // Only let the first core execute - mrc p15, 0, r3, c0, c0, 5 - and r3, r3, #3 - cmp r3, #0 - beq proceed - // this is a kind of blef - races can theoretically still occur - // when the main core overwrites this part of memory - wfe - -proceed: - // Initialize the stack (_stack_top is defined in linker.ld) - ldr sp, =_stack_top - - // Call kernel_main - ldr r3, =kernel_main - bx r3 diff --git a/demo_functionality.c b/demo_functionality.c index 4b002d6..420639b 100644 --- a/demo_functionality.c +++ b/demo_functionality.c @@ -53,7 +53,7 @@ void demo_current_mode(void) uart_puts(mode_name); } -#define TRANSLATION_TABLE \ +#define TRANSLATION_TABLE \ ((short_section_descriptor_t volatile*) TRANSLATION_TABLE_BASE) extern char @@ -144,29 +144,7 @@ void demo_go_unprivileged(void) write_SPSR(new_SPSR); uart_puts("All ready, jumping to PL0 code\n\r"); - + asm volatile("ldm %0, {r0 - r15} ^" :: "r" (PL0_regs)); } - -extern char - __interrupts_start, - __interrupts_end, - __interrupts_size; - -extern void (*volatile system_reentry_point)(void); - -void system_reentry(void) -{ - uart_puts("re-entered system"); - while(1); -} - -void demo_setup_interrupts(void) -{ - system_reentry_point = system_reentry; - - for (size_t i = 0; i < (size_t) &__interrupts_size; i++) - ((volatile char*) 0)[i] = - (&__interrupts_start)[i]; -} diff --git a/interrupt_vector.S b/interrupt_vector.S index d20bf6d..6037b7c 100644 --- a/interrupt_vector.S +++ b/interrupt_vector.S @@ -1,22 +1,45 @@ -.section ".interrupts.vector" - -.global abort_handler -.local generic_handler -.global _interrupt_vectors _interrupt_vectors: - b generic_handler - b generic_handler - b generic_handler + b reset_handler_caller + b undef_handler_caller + b svc_handler_caller b abort_handler_caller b abort_handler_caller - b generic_handler - b generic_handler - -.section ".interrupts.text" + b generic_handler_caller + b irq_handler_caller + b fiq_handler_caller + +reset_handler_caller: + ldr sp, =_stack_top + ldr r5, =reset_handler + bx r5 + +undef_handler_caller: + ldr sp, =_stack_top + ldr r5, =undefined_instruction_vector + bx r5 -generic_handler: - b generic_handler +svc_handler_caller: + ldr sp, =_stack_top + ldr r5, =supervisor_call_handler + bx r5 + abort_handler_caller: - mov sp, #0x8000 + ldr sp, =_stack_top ldr r5, =abort_handler bx r5 + +generic_handler_caller: + ldr sp, =_stack_top + ldr r5, =generic_handler + bx r5 + +irq_handler_caller: + ldr sp, =_stack_top + ldr r5, =irq_handler + bx r5 + +fiq_handler_caller: + ldr sp, =_stack_top + ldr r5, =fiq_handler + bx r5 + diff --git a/interrupts.c b/interrupts.c index 6952f89..1b0590a 100644 --- a/interrupts.c +++ b/interrupts.c @@ -1,10 +1,20 @@ #include "uart.h" -/** - @brief The undefined instruction interrupt handler - If an undefined instruction is encountered, the CPU will start - executing this function. Just trap here as a debug solution. -*/ +void setup(void); + +void reset_handler(void) +{ + static _Bool setup_done; + + if (!setup_done) + setup(); + + setup_done = 1; + + // TODO do something here + while(1); +} + void __attribute__((interrupt("UNDEF"))) __attribute__((section(".interrupts.text"))) @@ -17,13 +27,40 @@ undefined_instruction_vector(void) } } -void __attribute__((section(".interrupts.data"))) -(*system_reentry_point) (void); +void supervisor_call_handler(void) +{ + uart_puts("something svc happened\n\r"); + + while(1); +} void __attribute__((interrupt("ABORT"))) -__attribute__((section(".interrupts.text"))) abort_handler(void) { - system_reentry_point(); + uart_puts("re-entered system\n\r"); + + while(1); +} + +void generic_handler(void) +{ + uart_puts("something weird happened\n\r"); + + while(1); +} + +void irq_handler(void) +{ + uart_puts("irq happened\n\r"); + + while(1); +} + +void fiq_handler(void) +{ + uart_puts("fiq happened\n\r"); + + while(1); } + diff --git a/kernel.c b/kernel.c deleted file mode 100644 index 719ceff..0000000 --- a/kernel.c +++ /dev/null @@ -1,44 +0,0 @@ -#include "uart.h" -#include "demo_functionality.h" -#include "paging.h" - -void kernel_main(void) -{ - uart_init(); - - // When we attach screen session after loading kernel with socat - // we miss kernel's greeting... So we'll make the kernel wait for - // one char we're going to send from within screen - uart_getc(); - - uart_puts("Hello, kernel World!\r\n"); - - // prints some info - demo_paging_support(); - - // prints some info - demo_current_mode(); - - // prints some info and sets upp translation table, turns on MMU - setup_flat_map(); - - demo_setup_PL0(); - - demo_setup_interrupts(); - - // prints some info and sets up a section for PL0 code, loads a blob - // there and jumps to it... never, ever, ever returns - demo_go_unprivileged(); - - while(1); - - while (1) - { - char c = uart_getc(); - - uart_putc(c); - - if (c == '\r') - uart_putc('\n'); - } -} diff --git a/kernel_stage1.S b/kernel_stage1.S new file mode 100644 index 0000000..1e0f614 --- /dev/null +++ b/kernel_stage1.S @@ -0,0 +1,67 @@ +/* arm mode, cortex-a7 compatibility + * + * _boot is entry point for the kernel. + * + * Kernel copies it's embedded stage 2 to address 0x0 and jumps to + * it (to the reset handler). Registers r0 - r2 are arguments for + * the kernel, but we're not using them for now. + * + * This file is based on (and almost identical with) loader_stage1.S + */ + +.global _boot +_boot: + // Only let the first core execute + mrc p15, 0, r3, c0, c0, 5 + and r3, r3, #3 + cmp r3, #0 + beq proceed + // this is a kind of blef - races can theoretically still + // occur when the main core overwrites this part of memory + wfe + +proceed: + // copy stage2 of the kernel to address 0x0 + + // first, load address of stage2_start to r3 (a PIC way) + adr r3, stage2_start + + // load destination address for stage2 code to r4 + mov r4, #0 + + // load blob size to r5 + // The size might get too big for an immediate value, so + // we load it from memory. + adr r5, blob_size + ldr r5, [r5] + + // r6 is the counter - counts the bytes copied + mov r6, #0 + + // This initial piece of code might get overwritten when we + // copy stage2, so the actual copying loop shall be after + // stage2 blob. We want this asm code to be PIC, so we're + // computing address of stage2_end into r7. + add r7, r3, r5 + bx r7 + +blob_size: + .word stage2_end - stage2_start + +.align 4 +stage2_start: + .incbin "kernel_stage2.img" +stage2_end: + + // each word of the blob is loaded to r7 and stored + // from r7 to it's destination in a loop +loop: + ldr r7, [r3, r6] + str r7, [r4, r6] + add r6, r6, #4 + cmp r6, r5 + blo loop + + // Call stage2 of the kernel (branch to 0x0, + // which is the reset handler). + bx r4 diff --git a/kernel_stage1.ld b/kernel_stage1.ld new file mode 100644 index 0000000..3130634 --- /dev/null +++ b/kernel_stage1.ld @@ -0,0 +1,27 @@ +ENTRY(_boot) /* defined in boot.S; qemu needs it to run elf file */ + +/* Code starts at 0x8000 - that's where RPis in 32-bit mode load + * kernel at. My experiments do, however, show, that qemu emulating + * RPi2 loads the kernel at 0x10000! (took some pain to find out). + * rpi-open-firmware, on the other hand, loads kernel at 0x2000000! + * This is not really a problem, since: + * 1. We can use our bootloader to load the kernel at 0x8000 + * 2. We've rewritten stage 1 of both bootloader and kernel in + * careful assembly, so that they should work regardless of + * where they are loaded. + * 3. In qemu, we can load kernel.elf instead of raw binary + * (qemu will do the right thing then) + */ + +SECTIONS +{ + + . = 0x8000; + + __start = .; + .kernel_stage1 : + { + KEEP(kernel_stage1.o) + } + __end = .; +} diff --git a/kernel_stage2.ld b/kernel_stage2.ld new file mode 100644 index 0000000..d3a23bf --- /dev/null +++ b/kernel_stage2.ld @@ -0,0 +1,52 @@ +/* This sesond stage of the kernel is run from address 0x0 */ + +TRANSLATION_TABLE_SIZE = 4096 * 4; +MMU_SECTION_SIZE = 1 << 20; + +SECTIONS +{ + + . = 0x0; + + __start = .; + .kernel_stage2 : + { + KEEP(interrupt_vector.o) + . = ALIGN(4); + ramfs_embeddable.o + (*) + } + __end = .; + + . = ALIGN(1 << 14); + + .translation_table (NOLOAD) : + { + _translation_table_start = .; + + . = . + TRANSLATION_TABLE_SIZE; + + _translation_table_end = .; + } + + . = ALIGN(1 << 20); + . = . + MMU_SECTION_SIZE; + + .stack (NOLOAD) : + { + _stack_start = .; + + . = . + MMU_SECTION_SIZE; + + _stack_top = .; + } + + .unprivileged_memory (NOLOAD) : + { + _unprivileged_memory_start = .; + + . = . + MMU_SECTION_SIZE; + + _unprivileged_memory_end = .; + } +} diff --git a/linker.ld b/linker.ld deleted file mode 100644 index 444bbf6..0000000 --- a/linker.ld +++ /dev/null @@ -1,47 +0,0 @@ -ENTRY(_boot) /* defined in boot.S; qemu needs it to run elf file */ - -SECTIONS -{ - /* Starts at 0x8000 - that's where RPis in 32-bit mode load */ - /* kernel at. My experiments do, however, show, that qemu */ - /* emulating RPi2 loads the kernel at 0x10000! (took some pain */ - /* to find out). rpi-open-firmware, on the other hand, loads */ - /* kernel at 0x2000000! */ - /* This is not really a problem, since: */ - /* 1. We can use our bootloader to load the kernel at 0x8000 */ - /* 2. Stage 1 of the bootloader is written in careful */ - /* assembly, so that the loader itself should work */ - /* regardless of where it is loaded. */ - /* 3. In qemu, we can load kernel.elf instead of raw binary */ - /* (qemu will do the right thing then) */ - - . = 0x8000; - - /* RPi in 64-bit mode uses address 0x80000 instead */ - - __start = .; - .kernel : - { - __kernel_start = .; - KEEP(boot.o) - . = ALIGN(4); - ramfs_embeddable.o - *(EXCLUDE_FILE (libkernel.o interrupt_vector.o interrupts.o) *) - __kernel_end = .; - } - __kernel_size = __kernel_end - __kernel_start; - - .interrupts : - { - __interrupts_start = .; - KEEP(*(.interrupts.vector)) - interrupt_vector.o - interrupts.o - __interrupts_end = .; - } - __interrupts_size = __interrupts_end - __interrupts_start; - - __end = .; - - _stack_top = __start; -} diff --git a/memory.h b/memory.h index e4493e2..adc3bc0 100644 --- a/memory.h +++ b/memory.h @@ -1,7 +1,10 @@ #ifndef MEMORY_H #define MEMORY_H -#define POWER_OF_2(EXP) (((uint32_t) 1) << EXP) +// These macros were heavily used b4 I moved all the address +// computation to the linker script. Now I'm just keeping them +// in case they're needed for something else :) +#define POWER_OF_2(EXP) (((size_t) 1) << EXP) #define ALIGN_POWER_OF_2(ADDR, EXP) \ (((ADDR - 1) & ~(POWER_OF_2(EXP) - 1)) + POWER_OF_2(EXP)) @@ -10,33 +13,45 @@ #define ALIGN_SECTION(ADDR) ALIGN_POWER_OF_2(ADDR, 20) -#define INTERRUPT_VECTOR_TABLE_START ((uint32_t) 0x0) -#define STACK_START ((uint32_t) 0x4000) -#define STACK_END ((uint32_t) 0x8000) +// memory layout + +#define INTERRUPT_VECTOR_TABLE_START ((uint32_t) 0x0) +// all those symbols are defined in the linker script extern const char __end; extern const char __start; +extern const char _translation_table_start; +extern const char _translation_table_end; +extern const char _stack_start; +extern const char _stack_top; +extern const char _unprivileged_memory_start; +extern const char _unprivileged_memory_end; -#define KERNEL_START ((uint32_t) &__start) // this is 0x8000 -#define KERNEL_END ((uint32_t) &__end) +#define KERNEL_START ((size_t) &__start) // this is 0x0 +#define KERNEL_END ((size_t) &__end) // first 2^14 aligned address after the kernel -#define TRANSLATION_TABLE_BASE ALIGN_POWER_OF_2(KERNEL_END, 14) - -#define TRANSLATION_TABLE_END \ - (TRANSLATION_TABLE_BASE + (uint32_t) (4096 * 4)) +#define TRANSLATION_TABLE_BASE ((size_t) &_translation_table_start) +#define TRANSLATION_TABLE_END ((size_t) &_translation_table_end) -#define PRIVILEGED_MEMORY_END ALIGN_SECTION(TRANSLATION_TABLE_END) +// first section after the translation table is left unused; +// the next section is used as the stack +#define STACK_START ((size_t) &_stack_start) +#define STACK_END ((size_t) &_stack_top) -#define UNPRIVILEGED_MEMORY_START PRIVILEGED_MEMORY_END +#define PRIVILEGED_MEMORY_END STACK_END +#define UNPRIVILEGED_MEMORY_START \ + ((size_t) &_unprivileged_memory_start) // equal to STACK_END #define UNPRIVILEGED_MEMORY_END \ - (UNPRIVILEGED_MEMORY_START + SECTION_SIZE) + ((size_t) &_unprivileged_memory_end) -#define PL0_SECTION_NUMBER ((uint32_t) 0b101010101010) +#define PL0_SECTION_NUMBER ((size_t) 0xaaa) #define VIRTUAL_PL0_MEMORY_START (PL0_SECTION_NUMBER << 20) +#define VIRTUAL_PL0_MEMORY_END \ + (VIRTUAL_PL0_MEMORY_START + SECTION_SIZE) #endif // MEMORY_H diff --git a/setup.c b/setup.c new file mode 100644 index 0000000..48df825 --- /dev/null +++ b/setup.c @@ -0,0 +1,41 @@ +#include "uart.h" +#include "demo_functionality.h" +#include "paging.h" + +void setup(void) +{ + uart_init(); + + // When we attach screen session after loading kernel with socat + // we miss kernel's greeting... So we'll make the kernel wait for + // one char we're going to send from within screen + uart_getc(); + + uart_puts("Hello, kernel World!\r\n"); + + // prints some info + demo_paging_support(); + + // prints some info + demo_current_mode(); + + // prints some info and sets upp translation table, turns on MMU + setup_flat_map(); + + // prints some info and sets up a section for PL0 code, + // loads a blob there + demo_setup_PL0(); + + // jumps to unprivileged code... never, ever, ever returns + demo_go_unprivileged(); + + while (1) + { + char c = uart_getc(); + + uart_putc(c); + + if (c == '\r') + uart_putc('\n'); + } +} -- cgit v1.2.3 From a910d10349593fce9f5f28f0de4f27ba85cd7df2 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Sat, 28 Dec 2019 23:25:05 +0100 Subject: enter and exit supervisor call (+ fixed jumping to PL0 for the first time - setting sp was broken there) --- PL0_test.c | 15 +++++++++++---- demo_functionality.c | 17 ++++++++++++----- interrupt_vector.S | 4 +++- interrupts.c | 2 -- 4 files changed, 26 insertions(+), 12 deletions(-) (limited to 'interrupt_vector.S') diff --git a/PL0_test.c b/PL0_test.c index 32addf8..2e008a2 100644 --- a/PL0_test.c +++ b/PL0_test.c @@ -1,18 +1,25 @@ #include "uart.h" #include "psr.h" +// entry point - must remain the only function in the file! void PL0_main(void) { - // if all went correct, Success! gets printed + // If all went correct, Success! gets printed uart_puts("Success!\n\r"); - // if we're indeed i PL0, we should crash now, when trying to access + uart_puts("calling supervisor\n\r"); + + asm volatile("svc #0"); + + uart_puts("back from supervisor call\n\r"); + + // if we're indeed in PL0, we should crash now, when trying to access // memory we're not allowed to char first_kernel_byte[2]; - first_kernel_byte[0] = *(char*) ((uint32_t) 0x8000); + first_kernel_byte[0] = *(char*) ((uint32_t) 0x0); first_kernel_byte[1] = '\0'; - + uart_puts(first_kernel_byte); while (1) diff --git a/demo_functionality.c b/demo_functionality.c index 420639b..12bba9b 100644 --- a/demo_functionality.c +++ b/demo_functionality.c @@ -135,9 +135,8 @@ void *memset(void *s, int c, size_t n) void demo_go_unprivileged(void) { - uint32_t PL0_regs[16] = {0}; - PL0_regs[15] = VIRTUAL_PL0_MEMORY_START; // the new pc - PL0_regs[13] = (PL0_SECTION_NUMBER + 1) << 20; // the new sp + uint32_t PL0_regs[14] = {0}; + PL0_regs[13] = VIRTUAL_PL0_MEMORY_START; // the new pc PSR_t new_SPSR = read_CPSR(); new_SPSR.fields.PSR_MODE_4_0 = MODE_USER; @@ -145,6 +144,14 @@ void demo_go_unprivileged(void) uart_puts("All ready, jumping to PL0 code\n\r"); - asm volatile("ldm %0, {r0 - r15} ^" :: - "r" (PL0_regs)); + asm volatile("cps %[sysmode]\n\r" + "mov sp, %[stackaddr]\n\r" + "cps %[supmode]\n\r" + "ldm %[contextaddr], {r0 - r12, pc} ^" :: + [sysmode]"I" (MODE_SYSTEM), + [supmode]"I" (MODE_SUPERVISOR), + [stackaddr]"r" ((PL0_SECTION_NUMBER + 1) << 20), + [contextaddr]"r" (PL0_regs) : "memory"); + + __builtin_unreachable(); } diff --git a/interrupt_vector.S b/interrupt_vector.S index 6037b7c..88b32bf 100644 --- a/interrupt_vector.S +++ b/interrupt_vector.S @@ -21,7 +21,9 @@ undef_handler_caller: svc_handler_caller: ldr sp, =_stack_top ldr r5, =supervisor_call_handler - bx r5 + push {lr} + blx r5 + ldm sp!, {pc} ^ abort_handler_caller: ldr sp, =_stack_top diff --git a/interrupts.c b/interrupts.c index 1a0150e..7932d23 100644 --- a/interrupts.c +++ b/interrupts.c @@ -27,8 +27,6 @@ void undefined_instruction_vector(void) void supervisor_call_handler(void) { uart_puts("something svc happened\n\r"); - - while(1); } void abort_handler(void) -- cgit v1.2.3 From 0ec2016f80ffb5971362559b98b10744bb5bf893 Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 31 Dec 2019 18:54:28 +0100 Subject: split stack into irq, fiq and supervisor stacks; add proper handling of those in interrupt handlers (asm code) --- interrupt_vector.S | 25 ++++++++++++++----------- kernel_stage2.ld | 22 +++++++++++++++++++--- memory.h | 18 +++++++++++++++--- 3 files changed, 48 insertions(+), 17 deletions(-) (limited to 'interrupt_vector.S') diff --git a/interrupt_vector.S b/interrupt_vector.S index 88b32bf..af80eec 100644 --- a/interrupt_vector.S +++ b/interrupt_vector.S @@ -9,39 +9,42 @@ _interrupt_vectors: b fiq_handler_caller reset_handler_caller: - ldr sp, =_stack_top + ldr sp, =_supervisor_stack_top ldr r5, =reset_handler bx r5 undef_handler_caller: - ldr sp, =_stack_top + ldr sp, =_supervisor_stack_top ldr r5, =undefined_instruction_vector bx r5 svc_handler_caller: - ldr sp, =_stack_top + ldr sp, =_supervisor_stack_top + push {r5, lr} ldr r5, =supervisor_call_handler - push {lr} blx r5 - ldm sp!, {pc} ^ + ldm sp!, {r5, pc} ^ abort_handler_caller: - ldr sp, =_stack_top + ldr sp, =_supervisor_stack_top ldr r5, =abort_handler bx r5 generic_handler_caller: - ldr sp, =_stack_top + ldr sp, =_supervisor_stack_top ldr r5, =generic_handler bx r5 irq_handler_caller: - ldr sp, =_stack_top - ldr r5, =irq_handler - bx r5 + ldr sp, =_irq_stack_top + sub lr, #4 + push {r0-r3, lr} + ldr r3, =irq_handler + blx r3 + ldm sp!, {r0-r3, pc} ^ fiq_handler_caller: - ldr sp, =_stack_top + ldr sp, =_fiq_stack_top ldr r5, =fiq_handler bx r5 diff --git a/kernel_stage2.ld b/kernel_stage2.ld index a3df063..15b61ec 100644 --- a/kernel_stage2.ld +++ b/kernel_stage2.ld @@ -56,8 +56,24 @@ SECTIONS { _stack_start = .; - . = . + MMU_SECTION_SIZE; - - _stack_top = .; + _fiq_stack_start = .; + + . = . + (1 << 18); + + _fiq_stack_top = .; + + _irq_stack_start = .; + + . = . + (1 << 18); + + _irq_stack_top = .; + + _supervisor_stack_start = .; + + . = . + (1 << 19); + + _supervisor_stack_top = .; + + _stack_end = .; } } diff --git a/memory.h b/memory.h index c7952f0..bdeba52 100644 --- a/memory.h +++ b/memory.h @@ -28,7 +28,13 @@ extern char _translation_table_end; extern char _sections_list_start; extern char _sections_list_end; extern char _stack_start; -extern char _stack_top; +extern char _fiq_stack_start; +extern char _fiq_stack_top; +extern char _irq_stack_start; +extern char _irq_stack_top; +extern char _supervisor_stack_start; +extern char _supervisor_stack_top; +extern char _stack_end; #define KERNEL_START ((size_t) &__start) // this is 0x0 #define KERNEL_END ((size_t) &__end) @@ -43,8 +49,14 @@ extern char _stack_top; // first section after the translation table is left unused; // the next section is used as the stack -#define STACK_START ((size_t) &_stack_start) -#define STACK_END ((size_t) &_stack_top) +#define STACK_START ((size_t) &_stack_start) +#define FIQ_STACK_START ((size_t) &_fiq_stack_start) +#define FIQ_STACK_END ((size_t) &_fiq_stack_top) +#define IRQ_STACK_START ((size_t) &_irq_stack_start) +#define IRQ_STACK_END ((size_t) &_irq_stack_top) +#define SUPERVISOR_STACK_START ((size_t) &_supervisor_stack_start) +#define SUPERVISOR_STACK_END ((size_t) &_supervisor_stack_top) +#define STACK_END ((size_t) &_stack_end) #define PRIVILEGED_MEMORY_END STACK_END -- cgit v1.2.3