Skip to content

Commit 1be4e3b

Browse files
committed
init
1 parent 06979ed commit 1be4e3b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+4954
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.o
2+
*.elf
3+
kernel
4+
5+
include/arch
6+
include/driver
7+
8+
tftp_root/

Makefile

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
ARCH = riscv
2+
PREFIX = riscv64-unknown-linux-gnu-
3+
GCC = $(PREFIX)gcc
4+
OBJCOPY = $(PREFIX)objcopy
5+
OBJDUMP = $(PREFIX)objdump
6+
7+
DEFINES = -DPRINTF_DISABLE_SUPPORT_FLOAT -DPRINTF_DISABLE_SUPPORT_EXPONENTIAL \
8+
-DPRINTF_DISABLE_SUPPORT_LONG_LONG
9+
CFLAGS = -fno-builtin -nostdlib -static -Wl,--gc-sections,--print-gc-sections -O2 -Wall \
10+
-Iinclude $(DEFINES)
11+
LDFLAGS = -z separate-code
12+
13+
-include Makefile.config
14+
15+
.PHONY: all
16+
all:
17+
include arch/$(ARCH)/Makefrag
18+
19+
HEADERS=$(wildcard include/*.h) $(wildcard arch/$(ARCH)/include/*.h) $(wildcard driver/include/*.h)
20+
SOURCES=$(wildcard *.c *.S) $(wildcard arch/$(ARCH)/*.c arch/$(ARCH)/*.S) $(wildcard driver/*.c driver/*.S)
21+
OBJECTS=$(patsubst %.c,%.o,$(wildcard *.c)) $(patsubst %.S,%.o,$(wildcard *.S)) \
22+
$(patsubst %.c,%.o,$(wildcard arch/$(ARCH)/*.c)) $(patsubst %.S,%.o,$(wildcard arch/$(ARCH)/*.S)) \
23+
$(patsubst %.c,%.o,$(wildcard driver/*.c)) $(patsubst %.S,%.o,$(wildcard driver/*.S))
24+
25+
.PHONY: all
26+
all: $(OBJECTS) kernel tftp_root
27+
28+
.PHONY: asm
29+
asm: kernel
30+
$(OBJDUMP) -xd $< | vi -
31+
32+
include/arch:
33+
-rm $@
34+
ln -s ../arch/$(ARCH)/include include/arch
35+
36+
include/driver:
37+
-rm $@
38+
ln -s ../driver/include include/driver
39+
40+
%.o: %.c $(HEADERS) include/arch include/driver
41+
$(GCC) $(CFLAGS) -c $< -o $@
42+
43+
%.o: %.S $(HEADERS) include/arch include/driver
44+
$(GCC) $(CFLAGS) -c $< -o $@
45+
46+
kernel: $(OBJECTS) arch/$(ARCH)/linker.ld
47+
$(GCC) -Tarch/$(ARCH)/linker.ld $(CFLAGS) $(LDFLAGS) $^ -o $@
48+
49+
.PHONY: tftp_root
50+
tftp_root: kernel riscv_soc.dtb
51+
mkdir -p $@
52+
cp kernel $@/
53+
cp riscv_soc.dtb $@/dtb
54+
55+
.PHONY: clean
56+
clean:
57+
-rm *.o *.elf kernel arch/*/*.o
58+
-rm include/arch
59+
-rm include/driver
60+
-rm -r tftp_root

arch/amd64/Makefrag

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.PHONY: qemu
2+
qemu: kernel
3+
false
4+

arch/amd64/TODO

Whitespace-only changes.

arch/riscv/Makefrag

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CFLAGS += -mcmodel=medany
2+
3+
.PHONY: qemu
4+
qemu: kernel
5+
qemu-system-riscv64 -machine virt -nographic -kernel kernel -m 1024 -smp 1
6+

arch/riscv/include/asm.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef _ARCH_RISCV_ASM_H_
2+
#define _ARCH_RISCV_ASM_H_
3+
4+
#include "stdint.h"
5+
6+
static inline void fence()
7+
{
8+
asm volatile ("fence");
9+
}
10+
11+
static inline void fence_i()
12+
{
13+
asm volatile ("fence.i");
14+
}
15+
16+
static inline void flush_tlb()
17+
{
18+
asm volatile ("sfence.vma");
19+
}
20+
21+
static inline uint64_t rdcycle()
22+
{
23+
uint64_t v;
24+
asm volatile ("rdcycle %0" : "=r"(v));
25+
return v;
26+
}
27+
28+
#define CLOCK_FREQ 125000000ul
29+
30+
#define STACK_OFFSET (0)
31+
32+
#define DRAM_BASE 0x80000000ul
33+
#define DRAM_SIZE 0x40000000ul
34+
35+
#endif

arch/riscv/include/csr.h

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/*
2+
Copyright (c) 2013, The Regents of the University of California (Regents).
3+
All Rights Reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
1. Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
2. Redistributions in binary form must reproduce the above copyright
10+
notice, this list of conditions and the following disclaimer in the
11+
documentation and/or other materials provided with the distribution.
12+
3. Neither the name of the Regents nor the
13+
names of its contributors may be used to endorse or promote products
14+
derived from this software without specific prior written permission.
15+
16+
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
17+
SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
18+
OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
19+
BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20+
21+
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22+
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
24+
HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
25+
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
26+
*/
27+
28+
#ifndef _ARCH_RISCV_CSR_H_
29+
#define _ARCH_RISCV_CSR_H_
30+
31+
#define MSTATUS_UIE 0x00000001
32+
#define MSTATUS_SIE 0x00000002
33+
#define MSTATUS_HIE 0x00000004
34+
#define MSTATUS_MIE 0x00000008
35+
#define MSTATUS_UPIE 0x00000010
36+
#define MSTATUS_SPIE 0x00000020
37+
#define MSTATUS_HPIE 0x00000040
38+
#define MSTATUS_MPIE 0x00000080
39+
#define MSTATUS_SPP 0x00000100
40+
#define MSTATUS_HPP 0x00000600
41+
#define MSTATUS_MPP 0x00001800
42+
#define MSTATUS_VS 0x01800000
43+
#define MSTATUS_FS 0x00006000
44+
#define MSTATUS_XS 0x00018000
45+
#define MSTATUS_MPRV 0x00020000
46+
#define MSTATUS_SUM 0x00040000
47+
#define MSTATUS_MXR 0x00080000
48+
#define MSTATUS_TVM 0x00100000
49+
#define MSTATUS_TW 0x00200000
50+
#define MSTATUS_TSR 0x00400000
51+
#define MSTATUS_UXL 0x0000000300000000
52+
#define MSTATUS_SXL 0x0000000C00000000
53+
#define MSTATUS_SD 0x8000000000000000
54+
55+
#define SSTATUS_UIE 0x00000001
56+
#define SSTATUS_SIE 0x00000002
57+
#define SSTATUS_UPIE 0x00000010
58+
#define SSTATUS_SPIE 0x00000020
59+
#define SSTATUS_SPP 0x00000100
60+
#define SSTATUS_VS 0x01800000
61+
#define SSTATUS_FS 0x00006000
62+
#define SSTATUS_XS 0x00018000
63+
#define SSTATUS_SUM 0x00040000
64+
#define SSTATUS_MXR 0x00080000
65+
#define SSTATUS_UXL 0x0000000300000000
66+
#define SSTATUS_SD 0x8000000000000000
67+
68+
#define DCSR_XDEBUGVER (3U<<30)
69+
#define DCSR_NDRESET (1<<29)
70+
#define DCSR_FULLRESET (1<<28)
71+
#define DCSR_EBREAKM (1<<15)
72+
#define DCSR_EBREAKH (1<<14)
73+
#define DCSR_EBREAKS (1<<13)
74+
#define DCSR_EBREAKU (1<<12)
75+
#define DCSR_STOPCYCLE (1<<10)
76+
#define DCSR_STOPTIME (1<<9)
77+
#define DCSR_CAUSE (7<<6)
78+
#define DCSR_DEBUGINT (1<<5)
79+
#define DCSR_HALT (1<<3)
80+
#define DCSR_STEP (1<<2)
81+
#define DCSR_PRV (3<<0)
82+
83+
#define DCSR_CAUSE_NONE 0
84+
#define DCSR_CAUSE_SWBP 1
85+
#define DCSR_CAUSE_HWBP 2
86+
#define DCSR_CAUSE_DEBUGINT 3
87+
#define DCSR_CAUSE_STEP 4
88+
#define DCSR_CAUSE_HALT 5
89+
90+
#define MCONTROL_TYPE(xlen) (0xfULL<<((xlen)-4))
91+
#define MCONTROL_DMODE(xlen) (1ULL<<((xlen)-5))
92+
#define MCONTROL_MASKMAX(xlen) (0x3fULL<<((xlen)-11))
93+
94+
#define MCONTROL_SELECT (1<<19)
95+
#define MCONTROL_TIMING (1<<18)
96+
#define MCONTROL_ACTION (0x3f<<12)
97+
#define MCONTROL_CHAIN (1<<11)
98+
#define MCONTROL_MATCH (0xf<<7)
99+
#define MCONTROL_M (1<<6)
100+
#define MCONTROL_H (1<<5)
101+
#define MCONTROL_S (1<<4)
102+
#define MCONTROL_U (1<<3)
103+
#define MCONTROL_EXECUTE (1<<2)
104+
#define MCONTROL_STORE (1<<1)
105+
#define MCONTROL_LOAD (1<<0)
106+
107+
#define MCONTROL_TYPE_NONE 0
108+
#define MCONTROL_TYPE_MATCH 2
109+
110+
#define MCONTROL_ACTION_DEBUG_EXCEPTION 0
111+
#define MCONTROL_ACTION_DEBUG_MODE 1
112+
#define MCONTROL_ACTION_TRACE_START 2
113+
#define MCONTROL_ACTION_TRACE_STOP 3
114+
#define MCONTROL_ACTION_TRACE_EMIT 4
115+
116+
#define MCONTROL_MATCH_EQUAL 0
117+
#define MCONTROL_MATCH_NAPOT 1
118+
#define MCONTROL_MATCH_GE 2
119+
#define MCONTROL_MATCH_LT 3
120+
#define MCONTROL_MATCH_MASK_LOW 4
121+
#define MCONTROL_MATCH_MASK_HIGH 5
122+
123+
#define MIP_SSIP (1 << IRQ_S_SOFT)
124+
#define MIP_HSIP (1 << IRQ_H_SOFT)
125+
#define MIP_MSIP (1 << IRQ_M_SOFT)
126+
#define MIP_STIP (1 << IRQ_S_TIMER)
127+
#define MIP_HTIP (1 << IRQ_H_TIMER)
128+
#define MIP_MTIP (1 << IRQ_M_TIMER)
129+
#define MIP_SEIP (1 << IRQ_S_EXT)
130+
#define MIP_HEIP (1 << IRQ_H_EXT)
131+
#define MIP_MEIP (1 << IRQ_M_EXT)
132+
133+
#define SIP_SSIP MIP_SSIP
134+
#define SIP_STIP MIP_STIP
135+
136+
#define PRV_U 0
137+
#define PRV_S 1
138+
#define PRV_H 2
139+
#define PRV_M 3
140+
141+
#define PMP_R 0x01
142+
#define PMP_W 0x02
143+
#define PMP_X 0x04
144+
#define PMP_A 0x18
145+
#define PMP_L 0x80
146+
#define PMP_SHIFT 2
147+
148+
#define PMP_TOR 0x08
149+
#define PMP_NA4 0x10
150+
#define PMP_NAPOT 0x18
151+
152+
#define IRQ_S_SOFT 1
153+
#define IRQ_H_SOFT 2
154+
#define IRQ_M_SOFT 3
155+
#define IRQ_S_TIMER 5
156+
#define IRQ_H_TIMER 6
157+
#define IRQ_M_TIMER 7
158+
#define IRQ_S_EXT 9
159+
#define IRQ_H_EXT 10
160+
#define IRQ_M_EXT 11
161+
#define IRQ_COP 12
162+
#define IRQ_HOST 13
163+
164+
#define DEFAULT_RSTVEC 0x00001000
165+
#define CLINT_BASE 0x02000000
166+
#define CLINT_SIZE 0x000c0000
167+
168+
#ifdef __riscv
169+
170+
#ifndef __ASSEMBLER__
171+
172+
#ifdef __GNUC__
173+
174+
#define read_csr(reg) ({ unsigned long __tmp; \
175+
asm volatile ("csrr %0, " #reg : "=r"(__tmp)); \
176+
__tmp; })
177+
178+
#define write_csr(reg, val) ({ \
179+
asm volatile ("csrw " #reg ", %0" :: "rK"(val)); })
180+
181+
#define swap_csr(reg, val) ({ unsigned long __tmp; \
182+
asm volatile ("csrrw %0, " #reg ", %1" : "=r"(__tmp) : "rK"(val)); \
183+
__tmp; })
184+
185+
#define set_csr(reg, bit) ({ unsigned long __tmp; \
186+
asm volatile ("csrrs %0, " #reg ", %1" : "=r"(__tmp) : "rK"(bit)); \
187+
__tmp; })
188+
189+
#define clear_csr(reg, bit) ({ unsigned long __tmp; \
190+
asm volatile ("csrrc %0, " #reg ", %1" : "=r"(__tmp) : "rK"(bit)); \
191+
__tmp; })
192+
193+
#define rdtime() read_csr(time)
194+
#define rdcycle() read_csr(cycle)
195+
#define rdinstret() read_csr(instret)
196+
197+
#endif
198+
199+
#endif
200+
201+
#endif
202+
203+
#endif

0 commit comments

Comments
 (0)