diff options
author | Wojtek Kosior <kwojtus@protonmail.com> | 2019-12-02 16:37:22 +0100 |
---|---|---|
committer | Wojtek Kosior <kwojtus@protonmail.com> | 2019-12-02 16:37:22 +0100 |
commit | 401bf03070458cf3a5ee6947bd01a8f397fe2909 (patch) | |
tree | 568efb820ccf996118546108e98566584a3a904f | |
parent | 4b530611e8cd1a342a7e4461ac002cf715da26b3 (diff) | |
download | rpi-MMU-example-401bf03070458cf3a5ee6947bd01a8f397fe2909.tar.gz rpi-MMU-example-401bf03070458cf3a5ee6947bd01a8f397fe2909.zip |
create a separate library for kernel code that should run in PL0 mode
-rw-r--r-- | Makefile | 12 | ||||
-rw-r--r-- | PL0_test.c | 3 | ||||
-rw-r--r-- | demo_functionality.c | 51 | ||||
-rw-r--r-- | linker.ld | 41 | ||||
-rw-r--r-- | memory.h | 24 |
5 files changed, 113 insertions, 18 deletions
@@ -3,6 +3,10 @@ ELFFLAGS=-ffreestanding -O2 -nostdlib -lgcc -I. ARM_OBJECTS=kernel.o paging.o demo_functionality.o PL0_test.o uart.o loader_stage1.o loader_stage2.o +EMBEDDABLE_OBJECTS=PL_0_test_embeddable.o loader_stage2_embeddable.o + +RENAME_FLAGS=--rename-section .data=.renamed_data --rename-section .rodata=.renamed_rodata --rename-section .text=.renamed_text --rename-section .bss=.renamed_bss + all : kernel7.img %.o : %.c @@ -17,10 +21,16 @@ all : kernel7.img %_embeddable.o : %.img arm-none-eabi-objcopy -I binary -O elf32-littlearm -B arm --rename-section .data=.rodata $^ $@ +libkernel.o : libkernel.c + arm-none-eabi-gcc $(CFLAGS) -fPIC -c $^ -o $@ + +libkernel_renamed.o : libkernel.o + arm-none-eabi-objcopy $(RENAME_FLAGS) $^ $@ + 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 PL_0_test_embeddable.o demo_functionality.o paging.o +kernel.elf : boot.o kernel.o uart.o PL_0_test_embeddable.o demo_functionality.o paging.o libkernel_renamed.o arm-none-eabi-gcc -T linker.ld -o $@ $(ELFFLAGS) $^ loader_stage2.elf : loader_stage2.o uart.o @@ -5,9 +5,6 @@ void PL0_main(void) { uart_puts("hello PL0! Switching to user mode!\n\r"); - asm("cps #0b10000\n\r" - "isb" ::: "memory"); - // if all went correct, Success! gets printed uart_puts("Success!\n\r"); diff --git a/demo_functionality.c b/demo_functionality.c index eddb1d2..753a7d3 100644 --- a/demo_functionality.c +++ b/demo_functionality.c @@ -2,6 +2,7 @@ #include "psr.h" #include "memory.h" #include "translation_table_descriptors.h" +#include "libkernel.h" void demo_paging_support(void) { @@ -56,6 +57,11 @@ void demo_mode_to_system(void) } extern char + __renamed_start, + __renamed_end, + __renamed_size; + +extern char _binary_PL_0_test_img_start, _binary_PL_0_test_img_end, _binary_PL_0_test_img_size; @@ -65,15 +71,26 @@ void demo_go_unprivileged(void) short_section_descriptor_t *translation_table = (short_section_descriptor_t*) TRANSLATION_TABLE_BASE; + short_section_descriptor_t volatile *libkernel_section_entry = + &translation_table[LIBKERNEL_SECTION_NUMBER]; short_section_descriptor_t volatile *PL0_section_entry = &translation_table[PL0_SECTION_NUMBER]; short_section_descriptor_t volatile *UART_memory_section_entry = &translation_table[((uint32_t) GPIO_BASE) >> 20]; short_section_descriptor_t + libkernel_section = *libkernel_section_entry, PL0_section = *PL0_section_entry, UART_memory_section = *UART_memory_section_entry; + // make libkernel section executable and read-only for + // unprivileged code + libkernel_section.ACCESS_PERMISSIONS_2 = + AP_2_0_MODEL_RW_PL1_RO_PL0 >> 2; + libkernel_section.ACCESS_PERMISSIONS_1_0 = + AP_2_0_MODEL_RW_PL1_RO_PL0 & 0b011; + + // set up address of PL0 section PL0_section.SECTION_BASE_ADDRESS_31_20 = UNPRIVILEGED_MEMORY_START >> 20; @@ -88,6 +105,7 @@ void demo_go_unprivileged(void) UART_memory_section.ACCESS_PERMISSIONS_1_0 = AP_2_0_MODEL_RW_ALL & 0b011; + *libkernel_section_entry = libkernel_section; *PL0_section_entry = PL0_section; *UART_memory_section_entry = UART_memory_section; @@ -105,17 +123,32 @@ void demo_go_unprivileged(void) uart_puts((char*) VIRTUAL_PL0_MEMORY_START); - // now paste a userspace program to that section, jump to it and - // switch to PL0 + // copy libkernel code to libkernel section + for (size_t i = 0; i < (size_t) &__renamed_size; i++) + ((volatile char*) LIBKERNEL_SECTION_START)[i] = + (&__renamed_start)[i]; + + // now paste a userspace program to that section for (size_t i = 0; i < (size_t) &_binary_PL_0_test_img_size; i++) ((volatile char*) VIRTUAL_PL0_MEMORY_START)[i] = (&_binary_PL_0_test_img_start)[i]; - // jump to that copied code (switch to PL0 is done by that code) - asm volatile("mov r5, #0\n\r" - "movt r5, #"PL0_SECTION_NUMBER_STR"1111\n\r" - "mov sp, r5\n\r" // setting stack is important :D - "mov r5, #0\n\r" - "movt r5, #"PL0_SECTION_NUMBER_STR"0000\n\r" - "blx r5\n\r"); + uart_puts("copied PL0 and libkernel code to their sections"); + + size_t call_unprivileged_offset = + (size_t) &call_unprivileged - (size_t) &__renamed_start; + + void *call_unprivileged_new_location = + (void*) (LIBKERNEL_SECTION_START + call_unprivileged_offset); + + // call call_unprivileged from libkernel + asm volatile("mov r5, %0\n\r" + "mov r0, %1\n\r" + "mov r4, #0\n\r" + "movt r4, #"PL0_SECTION_NUMBER_STR"1111\n\r" + "mov sp, r4\n\r" // setting stack is important :D + "blx r5\n\r" :: + "r" (call_unprivileged_new_location), + "r" (VIRTUAL_PL0_MEMORY_START) + : "memory", "r4", "r5", "r0"); } @@ -49,5 +49,46 @@ SECTIONS . = ALIGN(4096); /* align to page size */ __bss_end = .; __bss_size = __bss_end - __bss_start; + + + + /* Here come the definitions for renamed sections */ + __renamed_start = .; + __renamed_text_start = .; + .renamed_text : + { + *(.renamed_text) + } + . = ALIGN(4096); /* align to page size */ + __renamed_text_end = .; + + __renamed_rodata_start = .; + .renamed_rodata : + { + *(.renamed_rodata) + } + . = ALIGN(4096); /* align to page size */ + __renamed_rodata_end = .; + + __renamed_data_start = .; + .renamed_data : + { + *(.renamed_data) + } + . = ALIGN(4096); /* align to page size */ + __renamed_data_end = .; + + __renamed_bss_start = .; + .renamed_bss : + { + renamed_bss = .; + *(.renamed_bss) + } + . = ALIGN(4096); /* align to page size */ + __renamed_bss_end = .; + __renamed_bss_size = __renamed_bss_end - __renamed_bss_start; + __renamed_end = .; + __renamed_size = __renamed_end - __renamed_start; + __end = .; } @@ -1,6 +1,10 @@ #ifndef MEMORY_H #define MEMORY_H +#include "paging.h" + +#define SECTION_SIZE (((uint32_t) 1) << 20) + #define INTERRUPT_VECTOR_TABLE_START ((uint32_t) 0x0) #define STACK_START ((uint32_t) 0x4000) @@ -20,11 +24,21 @@ extern char __start; #define TRANSLATION_TABLE_END \ (TRANSLATION_TABLE_BASE + (uint32_t) (4096 * 4)) -#define PRIVILEGED_MEMORY_END TRANSLATION_TABLE_END +#define LIBKERNEL_SECTION_START \ + (((TRANSLATION_TABLE_END - (uint32_t) 1) & ~((uint32_t) 0xfffff)) \ + + SECTION_SIZE) + +#define LIBKERNEL_SECTION_END \ + (LIBKERNEL_SECTION_START + SECTION_SIZE) -#define UNPRIVILEGED_MEMORY_START \ - (((PRIVILEGED_MEMORY_END - (uint32_t) 1) & ~((uint32_t) 0xfffff)) \ - + (uint32_t) 0x100000) +// section for libkernel is flat-mapped +#define LIBKERNEL_SECTION_NUMBER (LIBKERNEL_SECTION_START >> 20) + +#define PRIVILEGED_MEMORY_END LIBKERNEL_SECTION_END + +#define UNPRIVILEGED_MEMORY_START PRIVILEGED_MEMORY_END +#define UNPRIVILEGED_MEMORY_END \ + (UNPRIVILEGED_MEMORY_START + SECTION_SIZE) #define PL0_SECTION_NUMBER ((uint32_t) 0b101010101010) #define PL0_SECTION_NUMBER_STR "0b101010101010" @@ -32,4 +46,4 @@ extern char __start; #define VIRTUAL_PL0_MEMORY_START (PL0_SECTION_NUMBER << 20) #endif // MEMORY_H - + |