Skip to content

Commit 5c96b72

Browse files
committed
finally jumped to EL1
1 parent 21c6df6 commit 5c96b72

File tree

11 files changed

+221
-174
lines changed

11 files changed

+221
-174
lines changed

bottom/bootcode.bin

4 Bytes
Binary file not shown.

bottom/config.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

bottom/start.elf

3.63 KB
Binary file not shown.

os/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ objects := $(boot_dir)/*.o \
1515

1616
.PHONY: all $(modules) clean
1717

18-
all: clean $(modules) kernel7
18+
all: clean $(modules) kernel8
1919

20-
kernel7: $(modules)
20+
kernel8: $(modules)
2121
$(LD) $(objects) -T $(link_script) -e _start -o bootloader.elf
22-
$(OBJCOPY) bootloader.elf -O binary kernel7.img
22+
$(OBJCOPY) bootloader.elf -O binary kernel8.img
2323

2424
$(modules):
2525
$(MAKE) --directory=$@
@@ -29,7 +29,7 @@ clean:
2929
do \
3030
$(MAKE) --directory=$$d clean; \
3131
done; \
32-
rm -rf *.o *~ kernel7.img bootloader.elf
32+
rm -rf *.o *~ kernel8.img bootloader.elf
3333

3434
debug:
3535
$(OBJDUMP) -D -m aarch64 bootloader.elf > bootloader.list

os/boot/asm.s

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,76 @@
11
.globl _start
22
_start:
3-
B skip
4-
5-
.space 0x8000 - 0x4, 0
6-
7-
skip:
8-
MRS X0, mpidr_el1
9-
MOV X1, #0xC1000000
3+
MRS X0, MPIDR_EL1 // Check Core Id, we only use one core.
4+
MOV X1, #0XC1000000
105
BIC X0, X0, X1
116
CBZ X0, master
127
B hang
138

149
master:
15-
LDR X0, =0x04008000
10+
LDR X0, =0X04008000
1611
MOV SP, X0
1712
BL el3_main
1813

1914
hang:
2015
B hang
2116
22-
.globl el1_mmu_activate
23-
el1_mmu_activate:
24-
LDR X0, =0x04008000 // Set TTBR0
25-
MSR TTBR0_EL1, X0 // Set TTBR0
26-
MSR TCR_EL1, X2 // Set TCR
27-
ISB // The ISB forces these changes to be seen before the MMU is enabled.
17+
.globl get_current_el
18+
get_current_el:
19+
MRS X0, CURRENTEL
20+
RET
2821
29-
MRS X0, SCTLR_EL1 // Read System Control Register configuration data
22+
.globl EL1_mmu_activate
23+
EL1_mmu_activate:
24+
LDR X0, =0X04008000 // Set TTBR0
25+
MSR TTBR0_EL3, X0 // Set TTBR0
26+
LDR X2, =0X20018
27+
MSR TCR_EL3, X2 // Set TCR
28+
ISB // The ISB forces these changes to be seen before the MMU is enabled.
29+
RET
30+
TLBI ALLE3IS // Invalidate the entire TLB.
31+
MRS X0, SCTLR_EL3 // Read System Control Register configuration data.
3032
ORR X0, X0, #1 // Set [M] bit and enable the MMU.
31-
MSR SCTLR_EL1, X0 // Write System Control Register configuration data
33+
MSR SCTLR_EL3, X0 // Write System Control Register configuration data.
3234
ISB // The ISB forces these changes to be seen by the next instruction.
35+
RET
3336

3437
.globl jump_to_el1
3538
jump_to_el1:
36-
LDR X0, main
37-
MSR ELR_EL3, X0
38-
LDR X0, =0x03C08000
39-
MSR SP_EL1, X0 // Set SP
40-
MOV X0, #0x5
41-
MSR SPSR_EL3, X0
42-
ERET
39+
MRS X0, CURRENTEL // Check if already in EL1
40+
CMP X0, #4
41+
BEQ 1f
4342

43+
LDR X0, =0X03C08000
44+
MSR SP_EL1, X0 // Init the stack of EL1
45+
46+
// Disable coprocessor traps
47+
MOV X0, #0X33ff
48+
MSR CPTR_EL2, X0 // Disable coprocessor traps to EL2
49+
MSR HSTR_EL2, xzr // Disable coprocessor traps to EL2
50+
MOV X0, #3 << 20
51+
MSR CPACR_EL1, X0 // Enable FP/SIMD at EL1
52+
53+
// Initialize HCR_EL2
54+
MOV X0, #(1 << 31)
55+
MSR HCR_EL2, X0 // Set EL1 to 64 bit
56+
MOV X0, #0X0800
57+
MOVK X0, #0X30d0, LSL #16
58+
MSR SCTLR_EL1, X0
59+
60+
// Return to the EL1_SP1 mode from EL2
61+
MOV X0, #0X3C5
62+
MSR SPSR_EL2, X0 // EL1_SP0 | D | A | I | F
63+
ADR X0, 1f
64+
MSR ELR_EL2, X0
65+
ERET
66+
67+
1:
68+
MRS X0, SCTLR_EL1
69+
ORR X0, X0, #(1 << 12)
70+
MSR SCTLR_EL1, X0 // enable instruction cache
71+
72+
B main
73+
4474
.globl tlb_invalidate
4575
tlb_invalidate:
4676
DSB ISHST // ensure write has completed

os/boot/el3.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include "uart.h"
22
#include "pmap.h"
3+
#include "rpsio.h"
4+
#include "rpslib.h"
35

46
extern void el1_mmu_activate();
57
extern void jump_to_el1();
8+
extern u_long get_current_el();
69

710
void el3_main(void)
811
{
@@ -16,8 +19,12 @@ void el3_main(void)
1619
page_init();
1720

1821
// activate mmu for el1
19-
el1_mmu_activate();
22+
// el1_mmu_activate();
23+
printf("%lx\n", get_current_el());
2024

2125
// jump to el1
26+
printf("Jumping to EL1...\n");
2227
jump_to_el1();
28+
29+
panic("failed to jump");
2330
}

os/boot/main.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33
#include "gpio.h"
44
#include "pmap.h"
55

6-
int main (void)
6+
extern u_long get_current_el();
7+
8+
void main (void)
79
{
8-
printf("this is the start of el1_main\n");
10+
11+
printf("%lx\n", get_current_el());
12+
13+
printf("We are finally at EL1!\n");
914

1015
page_check();
1116

17+
printf("Page check passed!\n");
18+
1219
gpio_output_init(17);
1320
for (;;)
1421
{
@@ -18,6 +25,6 @@ int main (void)
1825
gpio_clr(17);
1926
sleep(1000);
2027
}
21-
22-
return 0;
28+
29+
panic("The end of main()\n");
2330
}

os/loader

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
MEMORY
33
{
4-
ram : ORIGIN = 0x0000, LENGTH = 0x1000000
4+
ram : ORIGIN = 0x80000, LENGTH = 0x1000000
55
}
66

77
SECTIONS

os/mm/include/mmu.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@
5454
o | |
5555
o | ... |
5656
o | |
57-
o +----------------------------+------------0x 0480 8000
57+
o UTOP -----> +----------------------------+------------0x 0430 9000
5858
o | PAGES |
59-
o UPAGES-----> +----------------------------+------------0x 0440 8000
59+
o UPAGES -----> +----------------------------+------------0x 0400 9000
6060
o | Page Table |
61-
o UVPT, EL3STACKTOP-----> +----------------------------+------------0x 0400 8000
61+
o UVPT, EL3STACKTOP -----> +----------------------------+------------0x 0400 8000
6262
o | EL3 stack |
63-
o KSTACKTOP-----> +----------------------------+------------0x 03C0 8000
64-
o | stack |
63+
o KSTACKTOP -----> +----------------------------+------------0x 03C0 8000
64+
o | Kernel stack |
6565
o | |
6666
o | Kernel Text |
67-
o KERNBASE-----> +----------------------------+------------0x 8000
67+
o KERNBASE -----> +----------------------------+------------0x 8 0000
6868
o | reserved |
6969
a 0 ------------> +----------------------------+------------0x 0000
7070
o
@@ -75,7 +75,8 @@
7575
#define KSTACKTOP EL3STACKTOP - 0x400000
7676
#define EL3STACKTOP 0x04008000
7777
#define UVPT EL3STACKTOP
78-
#define UPAGES UVPT + 0x400000
78+
#define UPAGES UVPT + 0x1000
79+
#define UTOP UPAGES + 0x300000
7980

8081
#define KSTKSIZE (60*1024*1024)
8182

os/mm/include/pmap.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ static inline u_long va2pa(Pte* pgdir, u_long va)
5757
{
5858
Pte* ppte;
5959
pgdir_walk(pgdir, va, 0, &ppte);
60-
return (u_long)ppte;
60+
if (!ppte || !(*ppte & PBE_V))
61+
{
62+
return ~0;
63+
}
64+
return (u_long)((Pte *)PTE_ADDR(*ppte));
6165
}
6266

6367
/* Get the Page struct whose physical address is 'pa'. */

0 commit comments

Comments
 (0)