From c173df46a0a12fe52fdf13323f967e9117462096 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Mon, 12 Aug 2024 08:24:44 +0200 Subject: Initial commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mainly with the code I took from @popovicu's blog. Signed-off-by: Miquel Sabaté Solà --- README.md | 2 ++ bios/Makefile | 25 +++++++++++++++++++++++++ bios/config.ld | 9 +++++++++ bios/hello.s | 23 +++++++++++++++++++++++ init/Makefile | 21 +++++++++++++++++++++ init/file_list.txt | 2 ++ init/hello.c | 8 ++++++++ init/little_shell | Bin 0 -> 1896643 bytes init/little_shell.go | 19 +++++++++++++++++++ init/shell.c | 32 ++++++++++++++++++++++++++++++++ kernel/Makefile | 20 ++++++++++++++++++++ kernel/hello.ld | 9 +++++++++ kernel/hello.s | 15 +++++++++++++++ kernel/infinite.ld | 9 +++++++++ kernel/infinite.s | 4 ++++ 15 files changed, 198 insertions(+) create mode 100644 README.md create mode 100644 bios/Makefile create mode 100644 bios/config.ld create mode 100644 bios/hello.s create mode 100644 init/Makefile create mode 100644 init/file_list.txt create mode 100644 init/hello.c create mode 100755 init/little_shell create mode 100644 init/little_shell.go create mode 100644 init/shell.c create mode 100644 kernel/Makefile create mode 100644 kernel/hello.ld create mode 100644 kernel/hello.s create mode 100644 kernel/infinite.ld create mode 100644 kernel/infinite.s diff --git a/README.md b/README.md new file mode 100644 index 0000000..83e9918 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +Experiments for `kernel`, `bios` and `init` taken from +[@popovicu](https://github.com/popovicu) and his blog. diff --git a/bios/Makefile b/bios/Makefile new file mode 100644 index 0000000..60e3cc4 --- /dev/null +++ b/bios/Makefile @@ -0,0 +1,25 @@ +NAME = hello + +CROSS_AS ?= riscv64-suse-linux-as +CROSS_LD ?= riscv64-suse-linux-ld +CROSS_DUMP ?= riscv64-suse-linux-objdump +ASFLAGS = -march=rv64g -mabi=lp64 + +EMU ?= qemu-system-riscv64 + +## +# Targets + +.PHONY: run +run: clean build + $(CROSS_DUMP) -D $(NAME) + $(EMU) -machine virt -bios hello + +.PHONY: build +build: + $(CROSS_AS) $(ASFLAGS) -o $(NAME).o -c $(NAME).s + $(CROSS_LD) -T config.ld $(NAME).o -o $(NAME) + +.PHONY: clean +clean: + @rm -f $(NAME).o $(NAME) diff --git a/bios/config.ld b/bios/config.ld new file mode 100644 index 0000000..085caa3 --- /dev/null +++ b/bios/config.ld @@ -0,0 +1,9 @@ +MEMORY { + dram_space (rwx) : ORIGIN = 0x80000000, LENGTH = 128 +} + +SECTIONS { + .text : { + hello.o(.text.bios) + } > dram_space +} diff --git a/bios/hello.s b/bios/hello.s new file mode 100644 index 0000000..f3aa20a --- /dev/null +++ b/bios/hello.s @@ -0,0 +1,23 @@ +# From: https://github.com/qemu/qemu/blob/23fa74974d8c96bc95cbecc0d4e2d90f984939f6/hw/riscv/virt.c#L82. +.set QEMU_UART0_ADDRESS, 0x10000000 + +.global _start +.section .text.bios + +_start: li a0, 0x68 + li a1, QEMU_UART0_ADDRESS + sb a0, (a1) # 'h' + + li a0, 0x65 + sb a0, (a1) # 'e' + + li a0, 0x6C + sb a0, (a1) # 'l' + + li a0, 0x6C + sb a0, (a1) # 'l' + + li a0, 0x6F + sb a0, (a1) # 'o' + +loop: j loop diff --git a/init/Makefile b/init/Makefile new file mode 100644 index 0000000..71405c3 --- /dev/null +++ b/init/Makefile @@ -0,0 +1,21 @@ +CROSS_CC ?= riscv64-suse-linux-gcc +CROSS_LD ?= riscv64-suse-linux-ld +CCFLAGS = -static + +## +# Targets + +.PHONY: hello +hello: clean + $(CROSS_CC) $(CCFLAGS) -o init hello.c + cpio -o -H newc < file_list.txt > initramfs.cpio + +.PHONY: shell +shell: clean + $(CROSS_CC) $(CCFLAGS) -o init shell.c + GOOS=linux GOARCH=riscv64 go build little_shell.go + cpio -o -H newc < file_list.txt > initramfs.cpio + +.PHONY: clean +clean: + @rm -f *.o init initramfs.cpio diff --git a/init/file_list.txt b/init/file_list.txt new file mode 100644 index 0000000..9dd8872 --- /dev/null +++ b/init/file_list.txt @@ -0,0 +1,2 @@ +init +little_shell diff --git a/init/hello.c b/init/hello.c new file mode 100644 index 0000000..d4e5892 --- /dev/null +++ b/init/hello.c @@ -0,0 +1,8 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + printf("Hello, world!\n"); + exit(0); +} diff --git a/init/little_shell b/init/little_shell new file mode 100755 index 0000000..5e2446c Binary files /dev/null and b/init/little_shell differ diff --git a/init/little_shell.go b/init/little_shell.go new file mode 100644 index 0000000..7324323 --- /dev/null +++ b/init/little_shell.go @@ -0,0 +1,19 @@ +package main + +import ( + "bufio" + "fmt" + "os" +) + +func main() { + fmt.Println("Hello world from Go!") + + reader := bufio.NewReader(os.Stdin) + + for { + fmt.Print("Enter your command: ") + line, _ := reader.ReadString('\n') + fmt.Printf("Your command is: %s", line) + } +} diff --git a/init/shell.c b/init/shell.c new file mode 100644 index 0000000..d44b1c7 --- /dev/null +++ b/init/shell.c @@ -0,0 +1,32 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + pid_t pid = fork(); + + if (pid == -1) { + printf("Unable to fork!"); + return -1; + } + + if (pid == 0) { + // This is a child process. + int status = execl("/little_shell", "irrelevant", NULL); + + if (status == -1) { + printf("Forked process cannot start the little_shell"); + return -2; + } + } + + int count = 1; + + while (1) { + printf("Hello from the original init! %d\n", count); + count++; + sleep(10); + } + + return 0; +} diff --git a/kernel/Makefile b/kernel/Makefile new file mode 100644 index 0000000..e943353 --- /dev/null +++ b/kernel/Makefile @@ -0,0 +1,20 @@ +CROSS_AS ?= riscv64-suse-linux-as +CROSS_LD ?= riscv64-suse-linux-ld +ASFLAGS = -march=rv64g -mabi=lp64 + +## +# Targets + +.PHONY: infinite +infinite: clean + $(CROSS_AS) $(ASFLAGS) -o infinite.o -c infinite.s + $(CROSS_LD) -T infinite.ld infinite.o -o infinite + +.PHONY: hello +hello: clean + $(CROSS_AS) $(ASFLAGS) -o hello.o -c hello.s + $(CROSS_LD) -T hello.ld hello.o -o hello + +.PHONY: clean +clean: + @rm -f *.o infinite hello diff --git a/kernel/hello.ld b/kernel/hello.ld new file mode 100644 index 0000000..e4af459 --- /dev/null +++ b/kernel/hello.ld @@ -0,0 +1,9 @@ +MEMORY { + kernel_space (rwx) : ORIGIN = 0x80200000, LENGTH = 128 +} + +SECTIONS { + .text : { + hello.o(.text.kernel) + } > kernel_space +} diff --git a/kernel/hello.s b/kernel/hello.s new file mode 100644 index 0000000..eacd797 --- /dev/null +++ b/kernel/hello.s @@ -0,0 +1,15 @@ +.global _start +.section .text.kernel + +_start: li a7, 0x4442434E + li a6, 0x00 + li a0, 12 + lla a1, debug_string + li a2, 0 + ecall + +loop: j loop + + .section .rodata +debug_string: + .string "Hello world\n" diff --git a/kernel/infinite.ld b/kernel/infinite.ld new file mode 100644 index 0000000..2eee384 --- /dev/null +++ b/kernel/infinite.ld @@ -0,0 +1,9 @@ +MEMORY { + kernel_space (rwx) : ORIGIN = 0x80200000, LENGTH = 128 +} + +SECTIONS { + .text : { + infinite.o(.text.kernel) + } > kernel_space +} diff --git a/kernel/infinite.s b/kernel/infinite.s new file mode 100644 index 0000000..855f58e --- /dev/null +++ b/kernel/infinite.s @@ -0,0 +1,4 @@ + .global _start + .section .text.kernel + +_start: j _start -- cgit v1.2.3