Skip to content

Commit

Permalink
Setup the stack for an init task
Browse files Browse the repository at this point in the history
Bootstrap an init task which holds at least the initial stack that is to
be used when setting up the registers (such as `sp` and `tp`).

In order to guarantee that the stack and the rest of the registers are
set up correctly, this commit also provides a raw implementation of a
`printk` function.

Moreover, this commit also adds an argument that must be passed to
`start_kernel`, which is the pointer to the embedded `fdt` blob. This
argument will be used by later work so to fetch, at least, the base
address for the initial ram disk. This was also used to test that the
stack was working as expected.

Signed-off-by: Miquel Sabaté Solà <[email protected]>
  • Loading branch information
mssola committed Nov 19, 2024
1 parent 24e9f07 commit bf0c41a
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 24 deletions.
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ endif
# no `-mcpu`, no `-mtune`, no funny business.

CC = $(CROSS_COMPILE)gcc$(CC_SUFFIX)
AS = $(CROSS_COMPILE)as$(CC_SUFFIX)
LD = $(CROSS_COMPILE)ld
QEMU ?= qemu-system-riscv64

ISA ?= rv64imafdc_zicntr_zicsr_zifencei_zihpm_zca_zcd_zba_zbb
ASFLAGS = -march=$(ISA) -mabi=lp64d
ASFLAGS = -march=$(ISA) -mabi=lp64d -mcmodel=medany
CCFLAGS = $(ASFLAGS) -Iinclude/
CCFLAGS += -Werror -Wpedantic -Wall -Wextra -Wcast-align -Wcast-qual -Winit-self \
-Wmissing-include-dirs -Wredundant-decls -Wshadow -Wsign-conversion \
-Wswitch-default -Wundef -Wunreachable-code -Wmissing-noreturn \
-Wswitch-default -Wundef -Wunreachable-code \
-nostdinc -nostdlib -std=gnu17
LDFLAGS = -Iinclude/ -static -melf64lriscv -z noexecstack
USRFLAGS = -static -melf64lriscv
Expand All @@ -48,14 +47,15 @@ ifeq ($(strip $(DEBUG)),)
CCFLAGS += -O3
QEMU_FLAGS += -nographic
else
ASFLAGS += -g
CCFLAGS += -g
QEMU_FLAGS += -s -S
endif

##
# Paths

SRC = $(wildcard kernel/head.S kernel/*.c)
SRC = $(filter-out kernel/fbos.ld.S, $(wildcard kernel/*.S kernel/*.c))
OBJ = $(patsubst %.c,%.o,$(patsubst %.S,%.o,$(SRC)))
LINKER = kernel/fbos.ld
KRNL = fbos
Expand Down Expand Up @@ -83,7 +83,7 @@ $(KRNL): $(OBJ) $(LINKER).S

.S.o:
$(E) " CC " $(*F)
$(Q) $(CC) $(CCFLAGS) -c $< -o $@
$(Q) $(CC) $(CCFLAGS) -D__ASSEMBLY__ -c $< -o $@

##
# User space
Expand All @@ -94,8 +94,8 @@ usr: $(USR)
$(Q) find usr/bin/ -type f -executable | cpio -o --quiet -H newc > $(INIT)

usr/src/%.o: usr/src/%.S
$(E) " AS " $(basename $@)
$(Q) $(AS) $(ASFLAGS) -c $< -o $@
$(E) " CC " $(basename $@)
$(Q) $(CC) $(ASFLAGS) -D__ASSEMBLY__ -c $< -o $@

usr/bin/%: usr/src/%.o
$(Q) mkdir -p usr/bin/
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ $ make gdb GDB_EXTRA_FLAGS="-tui"

And now you have started a GDB session with a nice TUI interface.

## Requirements

We do not want to support a myriad of different scenarios, but we want to keep
things simple. Hence, here there are some limitations/requirements:

- You need a recent enough OpenSBI running on your firmware. I have tested this
on a QEMU which has OpenSBI v1.5, but any firmware that implements a Runtime
SBI version of 2.0 should be fine.
- You are supposed to pass an `initrd` always. This kernel will not try to
magically come up with a made up file system or try to fetch something from an
existing one. An `initrd` is already provided for you on the default `make`
target, and that's what you are supposed to be passing to the kernel.

## Special thanks to

SUSE for organizing [Hack Week 24](https://hackweek.opensuse.org/24/projects).
Expand Down
23 changes: 22 additions & 1 deletion include/fbos/compiler.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
#ifndef __FBOS_COMPILER_H
#define __FBOS_COMPILER_H

/*
* Nicer looking versions of compiler attributes.
*/

#define __noreturn __attribute__((__noreturn__))

/*
* Compiler attributes specific to linker sections.
*/

#define __section(s) __attribute__((__section__(s)))
#define __kernel __section(".text.kernel")
#define __kernel __section(".kernel.text")

/*
* Multiple aliases for 64-bit integers which have their definition on the
* standard library.
*/

typedef long ssize_t;
typedef unsigned long size_t;
typedef unsigned long uint64_t;
typedef unsigned long uintptr_t;

// Helpful macro when prototyping.
#define __unused(x) (void)x

#endif /* __FBOS_COMPILER_H */
11 changes: 7 additions & 4 deletions include/fbos/init.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#ifndef FBOS_INIT_H
#define FBOS_INIT_H
#ifndef __FBOS_INIT_H
#define __FBOS_INIT_H

#include <fbos/compiler.h>

extern __noreturn __kernel void start_kernel(void);
extern struct task_struct init_task;

#endif /* FBOS_INIT_H */
// The entry point for the kernel.
__noreturn __kernel void start_kernel(uintptr_t *dtb);

#endif /* __FBOS_INIT_H */
13 changes: 10 additions & 3 deletions include/fbos/mm.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
#ifndef FBOS_MM_H
#define FBOS_MM_H
#ifndef __FBOS_MM_H
#define __FBOS_MM_H

/*
* Page = 4KB.
*/
#define PAGE_SIZE 0x1000

/*
* Initial size of the thread, which coincides with the size of the stack for a
* given thread.
*/
#define THREAD_SIZE_ORDER 2
#define THREAD_SIZE (PAGE_SIZE << THREAD_SIZE_ORDER)

/*
* The code will be linked to start at the first page, which will have a given
* offset.
*/
#define PAGE_OFFSET 0x80200000
#define LINK_ADDR PAGE_OFFSET

#endif /* FBOS_MM_H */
#endif /* __FBOS_MM_H */
12 changes: 7 additions & 5 deletions kernel/fbos.ld.S
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
SECTIONS {
// Ensure that the image starts at the very exact address SBI expects it to.
. = LINK_ADDR;
_start = .;
. = ALIGN(PAGE_SIZE);

// You would usually want to separate the head section into its own thing
// instead of clumping it into the main `.text` one. Well, I'm no expert on
// linker configuration, so patches are welcome :)
.text : {
// The very first thing has to be the `_start` function, which is where
// SBI will jump into. Afterwards comes the rest of `.text.head`.
_start = .;
*(.text.head)
_text = .;
*(.head.text)

// Aligning it to a full page is maybe a bit too much considering how
// small `.text.head` really is. I just saw this same thing on the Linux
Expand All @@ -23,7 +23,7 @@ SECTIONS {
// that large. That is, we put first the very core of the kernel, and
// the rest can go wherever.
__kernel_text_start = .;
*(.text.kernel)
*(.kernel.text)
__kernel_text_end = .;

// And the rest.
Expand All @@ -49,4 +49,6 @@ SECTIONS {
.bss : {
*(.bss)
}

_end = .;
}
55 changes: 52 additions & 3 deletions kernel/head.S
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
#include <fbos/mm.h>

.global _start
.section .text.head
.section .head.text

_start:
// TODO
call start_kernel
// Mask all interrupts
csrw sie, zero
csrw sip, zero

// Flush the instruction cache
fence.i

// Reset all registers except ra, a0, a1.
li sp, 0
li gp, 0
li tp, 0
li t0, 0
li t1, 0
li t2, 0
li s0, 0
li s1, 0
li a2, 0
li a3, 0
li a4, 0
li a5, 0
li a6, 0
li a7, 0
li s2, 0
li s3, 0
li s4, 0
li s5, 0
li s6, 0
li s7, 0
li s8, 0
li s9, 0
li s10, 0
li s11, 0
li t3, 0
li t4, 0
li t5, 0
li t6, 0
csrw sscratch, 0

// Point tp and sp to the init task.
la tp, init_task
la sp, init_task + THREAD_SIZE

// The `start_kernel` function requires an argument to be passed, which is
// the pointer to the `fdt` blob. The bootloader puts this on the `a1`
// register, so let's move it now to `a0`.
mv a0, a1

// Start the kernel.
tail start_kernel
18 changes: 17 additions & 1 deletion kernel/main.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
#include <fbos/init.h>
#include <fbos/printk.h>
#include <fbos/mm.h>
#include <fbos/sched.h>
#include <fbos/dt.h>

unsigned long init_stack[THREAD_SIZE / sizeof(unsigned long)];

struct task_struct init_task = { .stack = init_stack };

/*
* This is the main entry point of the kernel after head.S is done. This
* function can (and will) assume that everything has been reset and that we can
* start the whole thing.
*/
__noreturn __kernel void start_kernel(void)
__noreturn __kernel void start_kernel(uintptr_t *dtb)
{
// TODO: disable irqs, etc.

printk("Welcome to FizzBuzz OS!\n");

parse_dtb(dtb);

// TODO: reenable stuff

for (;;)
;
}

0 comments on commit bf0c41a

Please sign in to comment.