Skip to content

Commit 21297fb

Browse files
committed
lab3 finished
1 parent d292781 commit 21297fb

File tree

20 files changed

+454
-405
lines changed

20 files changed

+454
-405
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Porting MIPS operating system kernel to Raspberry Pi 3 on AArch64.
66
## TODO
77
- [X] Boot
88
- [X] Memory management
9-
- [ ] Process management and exception handling
9+
- [X] Process management and exception handling
1010
- [ ] System call
1111
- [ ] File system
1212
- [ ] Shell

os/Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ driver_dir := driver
33
boot_dir := boot
44
lib_dir := lib
55
mm_dir := mm
6+
user_dir := user
67

78
link_script := loader
89

9-
modules := boot driver lib mm
10+
modules := boot driver lib mm user
1011
objects := $(boot_dir)/*.o \
1112
$(driver_dir)/*.o \
1213
$(lib_dir)/*.o \
13-
$(mm_dir)/*.o
14+
$(mm_dir)/*.o \
15+
$(user_dir)/*.o
1416

1517

1618
.PHONY: all $(modules) clean

os/boot/asm.s

Lines changed: 81 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,50 @@
22

33
.globl _start
44
_start:
5-
mrs x0, mpidr_el1 // check core id, we only use one core.
5+
mrs x0, mpidr_el1 // check core id, only one core is used.
66
mov x1, #0xc1000000
77
bic x0, x0, x1
88
cbz x0, master
99
b hang
1010

1111
master:
1212
ldr x0, =0x1000000
13-
mov sp, x0 // set el3 sp
13+
mov sp, x0 // set el2 sp
1414
bl el2_main
1515

1616
hang:
1717
b hang
18-
18+
1919
.globl get_current_el
2020
get_current_el:
2121
mrs x0, currentel
2222
mov x1, #2
2323
lsr x0, x0, #2
2424
ret
25-
25+
2626
.globl el1_mmu_activate
2727
el1_mmu_activate:
2828
ldr x0, =0x04cc
2929
msr mair_el1, x0
3030
isb
3131

32-
ldr x1, =0x02000000
32+
ldr x1, =0x01000000
3333
msr ttbr0_el1, x1
3434
isb
3535

3636
mrs x2, tcr_el1
3737
ldr x3, =0x70040ffbf
3838
bic x2, x2, x3
3939

40-
ldr x3, =0x200803f18
40+
ldr x3, =0x2bf183f18
4141
orr x2, x2, x3
4242
msr tcr_el1, x2
4343
isb
4444

4545
mrs x3, sctlr_el1
4646
ldr x4, =0x80000
4747
bic x3, x3, x4
48-
48+
4949
ldr x4, =0x1005
5050
orr x3, x3, x4
5151
msr sctlr_el1, x3
@@ -54,45 +54,68 @@ el1_mmu_activate:
5454

5555
.globl jump_to_el1
5656
jump_to_el1:
57-
mrs x0, currentel // check if already in el1
58-
cmp x0, #4
59-
beq 1f
57+
mrs x0, currentel // check if already in el1
58+
cmp x0, #4
59+
beq 1f
6060

61-
ldr x0, =0xf00000
62-
msr sp_el1, x0 // init the stack of el1
61+
ldr x0, =0xf00000
62+
msr sp_el1, x0 // init the stack of el1
6363

6464
// disable coprocessor traps
65-
mov x0, #0x33ff
66-
msr cptr_el2, x0 // disable coprocessor traps to el2
67-
msr hstr_el2, xzr // disable coprocessor traps to el2
68-
mov x0, #3 << 20
69-
msr cpacr_el1, x0 // enable fp/simd at el1
70-
71-
// initialize hcr_el2
72-
mov x0, #(1 << 31)
73-
msr hcr_el2, x0 // set el1 to 64 bit
74-
mov x0, #0x0800
75-
movk x0, #0x30d0, lsl #16
76-
msr sctlr_el1, x0
77-
78-
// return to the el1_sp1 mode from el2
79-
mov x0, #0x3c5
80-
msr spsr_el2, x0 // el1_sp0 | d | a | i | f
81-
adr x0, 1f
82-
msr elr_el2, x0
83-
eret
84-
65+
mov x0, #0x33ff
66+
msr cptr_el2, x0 // disable coprocessor traps to el2
67+
msr hstr_el2, xzr // disable coprocessor traps to el2
68+
mov x0, #3 << 20
69+
msr cpacr_el1, x0 // enable fp/simd at el1
70+
71+
// initialize hcr_el2
72+
mov x0, #(1 << 31)
73+
msr hcr_el2, x0 // set el1 to 64 bit
74+
mov x0, #0x0800
75+
movk x0, #0x30d0, lsl #16
76+
msr sctlr_el1, x0
77+
78+
// return to the el1_sp1 mode from el2
79+
mov x0, #0x5
80+
msr spsr_el2, x0 // el1_sp1 with DAIF = 0
81+
adr x0, 1f
82+
msr elr_el2, x0
83+
eret
84+
8585
1:
86-
mrs x0, sctlr_el1
87-
orr x0, x0, #(1 << 12)
88-
msr sctlr_el1, x0 // enable instruction cache
86+
mrs x0, sctlr_el1
87+
orr x0, x0, #(1 << 12)
88+
msr sctlr_el1, x0 // enable instruction cache
89+
90+
ldr x0, =vectors
91+
msr vbar_el1, x0 // init exception vector table
92+
8993
b main
90-
94+
95+
.globl jump_to_el0
96+
jump_to_el0:
97+
mov x1, x0
98+
mrs x0, currentel // check if in el2
99+
cmp x0, #8
100+
beq 2f
101+
102+
ldr x0, =0xe00000
103+
msr sp_el0, x0 // init the stack of el0
104+
105+
// return to el0 from el1
106+
mov x0, #0x0
107+
msr spsr_el1, x0 // el0 with DAIF = 0
108+
msr elr_el1, x1
109+
110+
eret
111+
112+
2:
113+
ret
114+
91115
.globl tlb_invalidate
92116
tlb_invalidate:
93117
dsb ishst // ensure write has completed
94-
ldr x0, [x0] // load va from x0
95-
tlbi vmalle1 // invalidate tlb by va, all asid, el1.
118+
tlbi vmalle1is // invalidate tlb, all asid, el1.
96119
dsb ish // ensure completion of tlb invalidation
97120
isb // synchronize context and ensure that no instructions
98121
// are fetched using the old translation
@@ -111,3 +134,23 @@ get32:
111134
.globl dummy
112135
dummy:
113136
ret
137+
138+
.globl load_ttbr1_context
139+
load_ttbr1_context:
140+
msr ttbr1_el1, x0
141+
ret
142+
143+
.globl load_ttbr0_context
144+
load_ttbr0_context:
145+
msr ttbr0_el1, x0
146+
ret
147+
148+
.globl get_sctlr_el1
149+
get_sctlr_el1:
150+
mrs x0, sctlr_el1
151+
ret
152+
153+
.globl get_esr
154+
get_esr:
155+
mrs x0, esr_el1
156+
ret

os/boot/el2.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "uart.h"
22
#include "pmap.h"
3+
#include "env.h"
34
#include "rpsio.h"
45
#include "rpslib.h"
56

@@ -10,18 +11,23 @@ void el2_main(void)
1011
{
1112
// initialize uart
1213
uart_init();
13-
14-
printf("\n\nInitializing...\n");
14+
_printf("\n\nInitializing...\n");
1515

1616
// create page dir and page map
1717
vm_init();
18-
printf("vm_init success.\n");
18+
_printf("vm_init success.\n");
1919

2020
// fill page map
2121
page_init();
22+
_printf("page_init success.\n");
23+
24+
// init env structs
25+
env_init();
26+
_printf("env_init success.\n");
2227

2328
// jump to el1
24-
printf("Now in EL%lx. Jumping to EL1...\n", get_current_el());
29+
_printf("\n");
30+
_printf("Now in EL%lx. Jumping to EL1...\n", get_current_el());
2531
jump_to_el1();
2632

2733
panic("failed to jump EL");

os/boot/main.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
#include "rpsio.h"
22
#include "rpslib.h"
33
#include "gpio.h"
4+
#include "timer.h"
45
#include "pmap.h"
6+
#include "env.h"
57

68
extern u_long get_current_el();
79
extern void el1_mmu_activate();
810

11+
extern u_char userA[];
12+
extern u_char userB[];
13+
914
void main(void)
1015
{
11-
printf("We are now at EL%lx.\n", get_current_el());
12-
13-
// activate mmu for el1
14-
// el1_mmu_activate();
15-
// printf("MMU activated.\n");
16+
// activate el1/0 mmu
17+
el1_mmu_activate();
1618

17-
//printf("%s\n", "Page check start");
18-
//page_check();
19-
//printf("Page check passed!\n\n\n\n");
19+
_printf("We are now at EL%lx.\n", get_current_el());
20+
_printf("\n");
2021

21-
printf("Activating LED...\n");
2222
gpio_output_init(17);
23+
gpio_output_init(27);
24+
25+
_printf("creating new env...\n");
26+
env_create(userA, 66816);
27+
env_create(userB, 66816);
28+
29+
_printf("activating system timer...\n");
30+
set_system_timer_irq(1000000);
31+
32+
_printf("system initialization finished.\n");
33+
2334
for ( ; ; )
2435
{
25-
gpio_set(17);
26-
sleep(1000);
27-
28-
gpio_clr(17);
29-
sleep(1000);
3036
}
3137

3238
panic("The end of main()\n");

os/driver/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

2-
INCLUDES := -I../lib/include/ -I include/
2+
INCLUDES := -I../lib/include/ -I include/ -I../include/
33

44
%.o: %.c
55
$(CC) $(CFLAGS) $(INCLUDES) -c $<
66

77
.PHONY: clean
88

9-
all: uart.o gpio.o
9+
all: uart.o gpio.o timer.o
1010

1111
clean:
1212
rm -rf *~ *.o

os/driver/gpio.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
#include "rpslib.h"
22
#include "rpsio.h"
33

4-
#define MIN_GPIO 2
5-
#define MAX_GPIO 27
4+
#define MIN_GPIO 2
5+
#define MAX_GPIO 27
66

7-
#define PHYSADDR_OFFSET 0x3F200000
7+
#define PHYSADDR_OFFSET 0x3F200000
88

9-
#define GPFSEL0 (PHYSADDR_OFFSET + 0x00000000)
10-
#define GPSET0 (PHYSADDR_OFFSET + 0x0000001C)
11-
#define GPCLR0 (PHYSADDR_OFFSET + 0x00000028)
9+
#define GPFSEL0 (PHYSADDR_OFFSET + 0x00000000)
10+
#define GPSET0 (PHYSADDR_OFFSET + 0x0000001C)
11+
#define GPCLR0 (PHYSADDR_OFFSET + 0x00000028)
1212

1313
extern unsigned int get32(unsigned int);
1414
extern void put32(unsigned int, unsigned int);
1515

1616
static int check_if_gpio_legal(int n)
1717
{
18-
if ((n >= MIN_GPIO) && (n <= MAX_GPIO))
19-
{
20-
return 1;
21-
}
22-
else
23-
{
24-
return 0;
25-
}
18+
return n >= MIN_GPIO && n <= MAX_GPIO;
2619
}
2720

2821

@@ -40,7 +33,7 @@ void gpio_output_init(int n)
4033
ra &= ~(7 << ((n % 10) * 3));
4134
ra |= 1 << ((n % 10) * 3);
4235
put32(GPFSEL0 + 4 * (n / 10), ra);
43-
sleep(0);
36+
_sleep(0);
4437
}
4538

4639

@@ -52,7 +45,7 @@ void gpio_set(int n)
5245
}
5346

5447
put32(GPSET0 + 4 * (n / 32), 1 << (n % 32));
55-
sleep(0);
48+
_sleep(0);
5649
}
5750

5851

@@ -64,5 +57,5 @@ void gpio_clr(int n)
6457
}
6558

6659
put32(GPCLR0 + 4 * (n / 32), 1 << (n % 32));
67-
sleep(0);
60+
_sleep(0);
6861
}

os/driver/uart.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ void uart_init(void)
4343
ra |= 2 << 15; //alt5
4444
put32(GPFSEL1, ra);
4545
put32(GPPUD, 0);
46-
sleep(0);
46+
_sleep(0);
4747
put32(GPPUDCLK0, (1 << 14) | (1 << 15));
48-
sleep(0);
48+
_sleep(0);
4949
put32(GPPUDCLK0, 0);
5050
put32(AUX_MU_CNTL_REG, 3);
5151
}

0 commit comments

Comments
 (0)