Skip to content

Commit e874394

Browse files
mmu lowerhalf enabled
0 parents  commit e874394

36 files changed

+958
-0
lines changed

.vscode/ipch/41dae028f18889eb/el.ipch

1.3 MB
Binary file not shown.
8 Bytes
Binary file not shown.
1.3 MB
Binary file not shown.
8 Bytes
Binary file not shown.
8 Bytes
Binary file not shown.
1020 KB
Binary file not shown.
8 Bytes
Binary file not shown.
1.3 MB
Binary file not shown.
8 Bytes
Binary file not shown.
1.3 MB
Binary file not shown.
8 Bytes
Binary file not shown.
1.3 MB
Binary file not shown.
8 Bytes
Binary file not shown.
8 Bytes
Binary file not shown.

Makefile

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
drivers_dir := drivers
2+
boot_dir := boot
3+
init_dir := init
4+
lib_dir := lib
5+
mm_dir := mm
6+
vmlinux_elf := kernel.elf
7+
vmlinux_img := kernel8.img
8+
link_script := kernel.lds
9+
10+
modules := boot drivers init lib
11+
objects := $(boot_dir)/*.o \
12+
$(init_dir)/*.o \
13+
$(drivers_dir)/rpi3/*.o \
14+
$(lib_dir)/*.o
15+
16+
.PHONY: all $(modules) clean debug run
17+
18+
all: $(modules) vmlinux
19+
20+
vmlinux: $(modules)
21+
$(LD) -o $(vmlinux_elf) -T$(link_script) $(objects)
22+
$(CROSS_COMPILE)objcopy $(vmlinux_elf) -O binary $(vmlinux_img)
23+
24+
$(modules):
25+
$(MAKE) --directory=$@
26+
27+
clean:
28+
for d in $(modules); \
29+
do \
30+
$(MAKE) --directory=$$d clean; \
31+
done; \
32+
rm -rf *.o *~ $(vmlinux_elf) $(vmlinux_img)
33+
run:
34+
qemu-system-aarch64 -M raspi3 -serial stdio -kernel kernel8.img
35+
debug:
36+
qemu-system-aarch64 -S -s -M raspi3 -serial stdio -kernel kernel8.img
37+
include include.mk

boot/Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
INCLUDES := -I./ -I../ -I../include/
2+
3+
%.o: %.c
4+
$(CC) $(CFLAGS) $(INCLUDES) -c $<
5+
6+
%.o: %.S
7+
$(CC) $(CFLAGS) $(INCLUDES) -c $<
8+
9+
.PHONY: clean
10+
11+
all: start.o
12+
13+
clean:
14+
rm -rf *~ *.o
15+
16+
include ../include.mk

boot/start.S

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
.global _start
2+
3+
.section ".text.boot"
4+
_start:
5+
mrs x0, mpidr_el1
6+
and x0, x0, #3 // & 0b11
7+
cbz x0, _start_master
8+
1: wfe
9+
b 1b
10+
11+
_start_master:
12+
ldr x1, =_start
13+
// set up EL1
14+
mrs x0, CurrentEL
15+
and x0, x0, #12 // clear reserved bits
16+
17+
// running at EL3?
18+
cmp x0, #12
19+
bne 5f
20+
// should never be executed, just for completeness
21+
mov x2, #0x5b1
22+
msr scr_el3, x2
23+
mov x2, #0x3c9
24+
msr spsr_el3, x2
25+
adr x2, 5f
26+
msr elr_el3, x2
27+
eret
28+
29+
// running at EL2?
30+
5: cmp x0, #4
31+
beq 5f
32+
msr sp_el1, x1
33+
// enable CNTP for EL1
34+
/*it seems that this part is unnecessary in lab1
35+
and from the reference,P2172,it seems that this code can give the access
36+
to some registers from el1?*/
37+
mrs x0, cnthctl_el2
38+
orr x0, x0, #3
39+
msr cnthctl_el2, x0
40+
msr cntvoff_el2, xzr
41+
42+
// disable coprocessor traps
43+
mov x0, #0x33FF
44+
msr cptr_el2, x0 //essential! give access to SIMD,see reference 1891
45+
msr hstr_el2, xzr //seems not so useful. reference P1931
46+
mov x0, #(0xf << 20)//essential! give access to SIMD,see reference 3808
47+
msr cpacr_el1, x0
48+
// enable AArch64 in EL1
49+
mov x0, #(1 << 31) // set to AArch64
50+
orr x0, x0, #(1 << 1) // SWIO hardwired on Pi3,Reference 1928 ???
51+
msr hcr_el2, x0
52+
mrs x0, hcr_el2
53+
// Setup SCTLR access
54+
mov x2, #0x0800
55+
movk x2, #0x30d0, lsl #16//MOVK :move with keep
56+
msr sctlr_el1, x2
57+
// set up exception handlers
58+
//ldr x2, =_vectors
59+
//msr vbar_el1, x2
60+
// change execution level to EL1
61+
mov x2, #0x3c4
62+
msr spsr_el2, x2
63+
adr x2, 5f
64+
msr elr_el2, x2
65+
eret
66+
67+
5: //mov sp, x1
68+
69+
// clear bss
70+
ldr x1, =__bss_start
71+
ldr w2, =__bss_size
72+
3: cbz w2, 4f
73+
str xzr, [x1], #8
74+
sub w2, w2, #1
75+
cbnz w2, 3b
76+
77+
ldr x0, =0x80000
78+
mov sp, x0
79+
4:
80+
bl main
81+
b 1b

drivers/Makefile

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
drivers := rpi3
2+
3+
.PHONY: all $(drivers) clean
4+
5+
all: $(drivers)
6+
7+
$(drivers):
8+
$(MAKE) --directory=$@
9+
10+
clean:
11+
for d in $(drivers); \
12+
do \
13+
$(MAKE) --directory=$$d clean; \
14+
done
15+
16+
17+
include ../include.mk

drivers/rpi3/Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
%.o: %.c %.h
2+
$(CC) $(CFLAGS) -c $< -o $*.o
3+
4+
.PHONY: all clean
5+
6+
all: uart_boot.o
7+
8+
clean:
9+
rm -rf *.o *~
10+
11+
include ../../include.mk

drivers/rpi3/uart_boot.c

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
void uart_init_boot() {
3+
#define UART0_IBRD ((volatile unsigned int*)(0x3F201024))
4+
#define UART0_FBRD ((volatile unsigned int*)(0x3F201028))
5+
#define UART0_LCRH ((volatile unsigned int*)(0x3F20102C))
6+
#define UART0_CR ((volatile unsigned int*)(0x3F201030))
7+
#define UART0_ICR ((volatile unsigned int*)(0x3F201044))
8+
#define GPFSEL1 ((volatile unsigned int*)(0x3F200004))
9+
#define GPPUD ((volatile unsigned int*)(0x3F200094))
10+
#define GPPUDCLK0 ((volatile unsigned int*)(0x3F200098))
11+
#define UART0_DR ((volatile unsigned int*)(0x3F201000))
12+
#define UART0_FR ((volatile unsigned int*)(0x3F201018))
13+
register unsigned int r;
14+
*UART0_CR = 0;
15+
r = *GPFSEL1;
16+
r &= ~((7 << 12) | (7 << 15)); // gpio14, gpio15
17+
r |= (4 << 12) | (4 << 15); // alt0
18+
*GPFSEL1 = r;
19+
*GPPUD = 0; // enable pins 14 and 15
20+
r = 150;
21+
while (r--) { asm volatile("nop"); }
22+
*GPPUDCLK0 = (1 << 14) | (1 << 15);
23+
r = 150;
24+
while (r--) { asm volatile("nop"); }
25+
*GPPUDCLK0 = 0; // flush GPIO setup
26+
*UART0_ICR = 0x7FF; // clear interrupts
27+
*UART0_IBRD = 2; // 115200 baud
28+
*UART0_FBRD = 0xB;
29+
*UART0_LCRH = 0b11 << 5; // 8n1
30+
*UART0_CR = 0x301; // enable Tx, Rx, FIFO
31+
}
32+
33+
void uart_send_boot(unsigned int c) {
34+
do { asm volatile("nop"); } while (*UART0_FR & 0x20);
35+
*UART0_DR = c;
36+
}
37+
38+
39+
char uart_getc_boot() {
40+
char r;
41+
do { asm volatile("nop"); } while (*UART0_FR & 0x10);
42+
r = (char)(*UART0_DR);
43+
return r == '\r' ? '\n' : r;
44+
}

how_to_debug.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
modify the Makefile.Add -g to the arguements
2+
step1: in one terminal,
3+
make debug
4+
step2: in another terminal,
5+
aarch64-elf-gdb kernel.elf
6+
step3:(in gdb)
7+
target remote localhost:1234
8+
step4:
9+
break *0x80000
10+
continue
11+
till now ,we have reach the start of the kernel.
12+
we have two kinds of debug methods:assemble and c
13+
14+
c method:
15+
layout src //show the c original code
16+
step: one step forward(enter function)
17+
next: one step(won't enter function)
18+
19+
assemble method:
20+
layout asm// show asm code
21+
si one step forward(enter function)
22+
ni: one step(won't enter function)

include.mk

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CROSS_COMPILE := aarch64-elf-
2+
CC := $(CROSS_COMPILE)gcc
3+
CFLAGS := -Wall -ffreestanding -g
4+
LD := $(CROSS_COMPILE)ld

include/mmu.h

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef _mmu_h
2+
#define _mmu_h
3+
4+
extern char _end[];
5+
extern char _start[];
6+
extern char _pg_dir[];
7+
extern volatile unsigned char _data[];
8+
//memory class
9+
#define PTE_NORMAL (0<<2)
10+
#define PTE_DEVICE (1 << 2)
11+
#define PTE_NON_CACHE (2 << 2)
12+
13+
#define TCR_T0SZ (64 - 48)
14+
#define TCR_T1SZ ((64 - 48) << 16)
15+
#define TCR_TG0_4K (0 << 14)
16+
#define TCR_TG1_4K (2 << 30)
17+
#define TCR_VALUE (TCR_T0SZ | TCR_T1SZ | TCR_TG0_4K | TCR_TG1_4K)
18+
19+
#define BY2PG 4096
20+
#define MEM_SIZE 0x40000000
21+
22+
#define TYPE_PAGE 0x3
23+
#define TYPE_BLOCK 0x1
24+
#define MM_ACCESS_PERMISSION (0x01 << 6)
25+
#define PTE_V 0x1
26+
#define PTE_AF (0x1 << 10)
27+
#define PTE_RO (0x1 << 7)
28+
#define PTE_USER (0x1<<6)
29+
30+
#define PTE_OSH (2<<8) // outter shareable
31+
#define PTE_ISH (3<<8) // inner shareable
32+
33+
#define KERNEL_BASE 0xffff000000000000
34+
/* Rounding; only works for n = power of two */
35+
#define ROUND(a, n) (((((unsigned long)(a))+(n)-1)) & ~((n)-1))
36+
#define ROUNDDOWN(a, n) (((unsigned long)(a)) & ~((n)-1))
37+
38+
#define PGDX(va) ((((unsigned long)(va))>>39)&0x1ff)
39+
#define PUDX(va) ((((unsigned long)(va))>>30)&0x1ff)
40+
#define PMDX(va) ((((unsigned long)(va))>>21)&0x1ff)
41+
#define PTEX(va) ((((unsigned long)(va))>>12)&0x1ff)
42+
43+
44+
#endif

include/print.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) 2001 MontaVista Software Inc.
3+
* Author: Jun Sun, [email protected] or [email protected]
4+
*
5+
* This program is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License as published by the
7+
* Free Software Foundation; either version 2 of the License, or (at your
8+
* option) any later version.
9+
*
10+
*/
11+
12+
#ifndef _print_h_
13+
#define _print_h_
14+
15+
#include <stdarg.h>
16+
17+
/* this is the maximum width for a variable */
18+
#define LP_MAX_BUF 1000
19+
20+
/* -*-
21+
* output function takes an void pointer which is passed in as the
22+
* second argument in lp_Print(). This black-box argument gives output
23+
* function a way to track state.
24+
*
25+
* The second argument in output function is a pointer to char buffer.
26+
* The third argument specifies the number of chars to outputed.
27+
*
28+
* output function cannot assume the buffer is null-terminated after
29+
* l number of chars.
30+
*/
31+
void lp_Print(void (*output)(void *, char *, int),
32+
void * arg,
33+
char *fmt,
34+
va_list ap);
35+
36+
#endif

include/printf.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (C) 2001 MontaVista Software Inc.
3+
* Author: Jun Sun, [email protected] or [email protected]
4+
*
5+
* This program is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License as published by the
7+
* Free Software Foundation; either version 2 of the License, or (at your
8+
* option) any later version.
9+
*
10+
*/
11+
12+
#ifndef _printf_h_
13+
#define _printf_h_
14+
15+
#include <stdarg.h>
16+
void printf(char *fmt, ...);
17+
18+
void _panic(const char *, int, const char *, ...)
19+
__attribute__((noreturn));
20+
21+
#define panic(...) _panic(__FILE__, __LINE__, __VA_ARGS__)
22+
23+
#endif /* _printf_h_ */

include/types.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef _INC_TYPES_H_
2+
#define _INC_TYPES_H_
3+
4+
typedef signed short int16_t;
5+
typedef unsigned short u_int16_t;
6+
typedef signed int int32_t;
7+
typedef unsigned int u_int32_t;
8+
typedef signed long int64_t;
9+
typedef unsigned long u_int64_t;
10+
11+
typedef unsigned char u_char;
12+
typedef unsigned short u_short;
13+
typedef unsigned int u_int;
14+
typedef unsigned long u_long;
15+
16+
typedef unsigned long long size_t;
17+
18+
#ifndef NULL
19+
#define NULL ((void *) 0)
20+
#endif /* !NULL */
21+
22+
#define MIN(_a, _b) \
23+
({ \
24+
typeof(_a) __a = (_a); \
25+
typeof(_b) __b = (_b); \
26+
__a <= __b ? __a : __b; \
27+
})
28+
29+
/* Static assert, for compile-time assertion checking */
30+
#define static_assert(c) switch (c) case 0: case(c):
31+
32+
#define offsetof(type, member) ((size_t)(&((type *)0)->member))
33+
34+
/* Rounding; only works for n = power of two */
35+
#define ROUND(a, n) (((((u_long)(a))+(n)-1)) & ~((n)-1))
36+
#define ROUNDDOWN(a, n) (((u_long)(a)) & ~((n)-1))
37+
38+
#endif /* !_INC_TYPES_H_ */

0 commit comments

Comments
 (0)