diff --git a/kernel/boot.S b/kernel/arch/riscv/entry.S similarity index 100% rename from kernel/boot.S rename to kernel/arch/riscv/entry.S diff --git a/kernel/arch/riscv/meson.build b/kernel/arch/riscv/meson.build new file mode 100644 index 0000000..808c949 --- /dev/null +++ b/kernel/arch/riscv/meson.build @@ -0,0 +1,9 @@ +arch_sources = [ + files( + 'entry.S' + ), + kernel_sources +] + +subdir('platform/' + platform) + diff --git a/kernel/arch/riscv/platform/allwinner-d1/meson.build b/kernel/arch/riscv/platform/allwinner-d1/meson.build new file mode 100644 index 0000000..867b244 --- /dev/null +++ b/kernel/arch/riscv/platform/allwinner-d1/meson.build @@ -0,0 +1,51 @@ +objcopy = find_program('riscv64-linux-gnu-objcopy') +mkimage = find_program('mkimage') + +arch_sources += [ + +] + +elf = executable( + 'kernel.elf', + arch_sources, + link_args: [ + '-Wl,-T,' + meson.current_source_dir() + '/platform.ld', + '-static' + ], + link_depends: files('platform.ld'), + install: true, + install_dir: meson.build_root() +) + +bin = custom_target( + 'kernel.bin', + command: [objcopy, '-O', 'binary', '@INPUT@', '@OUTPUT@'], + input: elf, + output: 'kernel.bin' +) + +uboot_img = custom_target( + 'kernel.uboot', + command: [ + mkimage, + '-d', '@INPUT@', + '-A', 'riscv', + '-O', 'u-boot', + '-a', '0x45000000', + '-e', '0x45000000', + '-C', 'none', + '@OUTPUT@' + ], + input: bin, + output: 'kernel.uboot', + install: true, + install_dir: meson.build_root() +) + +run_target('uart', + command: [ + find_program(meson.source_root() + '/tools/uboot_upload'), + elf + ] +) + diff --git a/kernel/kernel.ld b/kernel/arch/riscv/platform/allwinner-d1/platform.ld similarity index 100% rename from kernel/kernel.ld rename to kernel/arch/riscv/platform/allwinner-d1/platform.ld diff --git a/kernel/arch/riscv/platform/qemu/meson.build b/kernel/arch/riscv/platform/qemu/meson.build new file mode 100644 index 0000000..979f286 --- /dev/null +++ b/kernel/arch/riscv/platform/qemu/meson.build @@ -0,0 +1,27 @@ +elf = executable( + 'kernel.elf', + arch_sources, + link_args: [ + '-Wl,-T,' + meson.current_source_dir() + '/platform.ld', + '-static' + ], + link_depends: files('platform.ld'), + install: true, + install_dir: meson.build_root() +) + +run_target('qemu', + command: [ + find_program('qemu-system-riscv64'), + '-machine', 'virt', + '-cpu', 'rv64', + '-bios', 'opensbi-riscv64-generic-fw_dynamic.bin', + '-m', '256m', + '-serial', 'stdio', + '-s', + '-display', 'none', + # '-d', 'int', + '-kernel', elf + ] +) + diff --git a/kernel/arch/riscv/platform/qemu/platform.ld b/kernel/arch/riscv/platform/qemu/platform.ld new file mode 100644 index 0000000..a9dd450 --- /dev/null +++ b/kernel/arch/riscv/platform/qemu/platform.ld @@ -0,0 +1,25 @@ +OUTPUT_ARCH("riscv") +OUTPUT_FORMAT("elf64-littleriscv") +ENTRY(_start) +SECTIONS { + /* R-X segment. */ + . = 0x81000000; + + .text : { *(.text) *(.text.*) } + + /* R-- segment. */ + . = ALIGN(0x1000) + (. & (0xFFF)); + /* For some reason, ld does not emit a read-only segment without an additional gap. */ + . += 0x1000; + + .rodata : { *(.rodata) *(.rodata.*) } + .note.gnu.build-id : { *(.note.gnu.build-id) } + + /* RW- segment. */ + . = ALIGN(0x1000) + (. & (0xFFF)); + + .data : { *(.data) *(.data.*) } + .got : { *(.got) } + .got.plt : { *(.got.plt) } + .bss : { *(.bss) *(.bss.*) } +} diff --git a/kernel/kernel.cpp b/kernel/kernel.cpp index 99d5a7e..0b98da4 100644 --- a/kernel/kernel.cpp +++ b/kernel/kernel.cpp @@ -281,11 +281,13 @@ extern "C" void kmain(unsigned long hart, void *dtb) { //fmt("test\n"); + /* volatile uint32_t *cfg = (uint32_t *)0x2000060; volatile uint32_t *dat = (uint32_t *)0x2000070; *cfg = 0x10; *dat = 0xFF; + */ fmt("y"); @@ -306,11 +308,11 @@ extern "C" void kmain(unsigned long hart, void *dtb) { cli(); unsigned long time; - volatile uint32_t *meme = (uint32_t *)0x6011010; + //volatile uint32_t *meme = (uint32_t *)0x6011010; //while(true) { time = read_csr("time"); fmt("hello world {}\n", time); - *meme = *meme | (0xA57 << 1) | (1 << 0); + //*meme = *meme | (0xA57 << 1) | (1 << 0); //} // Identity map the first 512GiB. @@ -339,9 +341,9 @@ extern "C" void kmain(unsigned long hart, void *dtb) { create_task(&task2, task2_main); time = read_csr("time"); - //sbi_call1(0x54494D45, 0, time + 100000); + sbi_call1(0x54494D45, 0, time + 100000); - //save_stack(switch_task, 0); + save_stack(switch_task, 0); sbi_call1(8, 0, 0); diff --git a/kernel/meson.build b/kernel/meson.build new file mode 100644 index 0000000..5cf1571 --- /dev/null +++ b/kernel/meson.build @@ -0,0 +1,8 @@ +kernel_sources = [ + files( + 'kernel.cpp' + ) +] + +subdir('arch/' + arch) + diff --git a/meson.build b/meson.build index c996ea6..6d2d7e0 100644 --- a/meson.build +++ b/meson.build @@ -1,26 +1,18 @@ -project('riscv-qtech-os', ['c', 'cpp']) - -kernel = executable('kernel', - 'kernel/boot.S', - 'kernel/kernel.cpp', - link_args: [ - '-Wl,-T,' + (meson.current_source_dir() / 'kernel/kernel.ld'), - '-static' +project('riscv-qtech-os', ['c', 'cpp'], + default_options: [ + 'cpp_std=gnu++20', + 'c_std=gnu11', + 'c_flags=-Wall -Werror -Wextra', + 'cpp_flags=-Wall -Werror -Wextra' ] ) -run_target('qemu', - command: [ - find_program('qemu-system-riscv64'), - '-machine', 'virt', - '-cpu', 'rv64', - '-bios', 'opensbi-riscv64-generic-fw_dynamic.bin', - '-m', '256m', - '-serial', 'stdio', - '-s', - '-display', 'none', - # '-d', 'int', - '-kernel', kernel - ] -) +if not meson.is_cross_build() + error('Kernel must be cross compiled') +endif +arch = host_machine.cpu_family() +platform = 'allwinner-d1' +# platform = 'qemu' + +subdir('kernel') diff --git a/tools/uboot_upload b/tools/uboot_upload new file mode 100755 index 0000000..d141690 --- /dev/null +++ b/tools/uboot_upload @@ -0,0 +1,7 @@ +#!/bin/bash + +stty -F /dev/ttyUSB0 115200 -echo -icrnl +echo "loadx" > /dev/ttyUSB0 +sx -vv -X -k $1 < /dev/ttyUSB0 > /dev/ttyUSB0 +echo "bootelf" > /dev/ttyUSB0 +cat - < /dev/ttyUSB0