Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
8e4f77b
feat(bulb): add bulb, an OS that solely relies on mailbox comms
anarchuser Nov 2, 2021
8efba79
chore(mmu): add logging to mmu init
anarchuser Nov 2, 2021
20e271c
chore(mmu): add some debugging...
anarchuser Nov 2, 2021
4852339
feat(bulb): add irq to show MMU calls sync invalid exception
anarchuser Nov 3, 2021
8342385
refactor(mmu): initialise PGD0 tables for kernel
anarchuser Nov 3, 2021
447dbd4
fix(mmu): correctly initialise tables, esp. peripheral base
anarchuser Nov 3, 2021
4a930b0
refactor(mmu): removed weird passing of r and b by reference
anarchuser Nov 3, 2021
c68ec37
feat(bulb): enable (timer, uart) interrupts
anarchuser Nov 3, 2021
c6f1c7f
feat(mmu): initialise and enable mmu on thorn
anarchuser Nov 3, 2021
b0db69a
feat(thorn): multiplex print to uart and screen
anarchuser Nov 3, 2021
6ac7e1b
feat(mmu): fix data abort el0
maxstreitberger Nov 4, 2021
d074e77
feat(mmu): WIP
maxstreitberger Nov 4, 2021
534240d
feat(mmu): WIP
maxstreitberger Nov 4, 2021
9b34694
feat(mmu): make all of non-kernel memory non-executable
anarchuser Nov 3, 2021
2c7ec97
refactor(mmu): misc changes for debugging; give ELR to data abort
anarchuser Nov 10, 2021
b121473
chore: figure out mmu gets stuck in schedule
anarchuser Nov 10, 2021
dee3563
WIP: figure out why user_process starts with sp = 0
anarchuser Nov 11, 2021
83813b7
WIP: try to figure out why user_process hangs immediately
anarchuser Nov 11, 2021
c1b826e
refactor(mmu): right now, everything runs fine
anarchuser Nov 11, 2021
735eb1b
fix(irq): move irq init into kernel_init
anarchuser Dec 14, 2021
3bcd28b
refactor(mmu): on data abort, kill offending process
anarchuser Dec 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ clean:
# Build the different OSes
build: build-$(BUILD_DEFAULT_TARGET)

buildall: build-chainloader build-thorn
buildall: build-chainloader build-thorn build-bulb

build-chainloader:
$(MAKE) -C chainloader build
Expand Down
8 changes: 7 additions & 1 deletion thorn/include/common/font.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef _ROSE_C_FONT_H
#define _ROSE_C_FONT_H

#ifndef __ASSEMBLER__

#define FONT_SIZE 8
#define FONT_FACTOR 2
#define FONT_FACTOR 3
#define FONT_REAL_WIDTH (FONT_SIZE * FONT_FACTOR)

// Spacing between lines in pixels
Expand Down Expand Up @@ -32,11 +34,15 @@ static volatile color_t font_fg;
const unsigned long long int * font (int c);

void printc (char c);
void prints (char const * str);
void printc_location (point_t point, char c);

void putc_screen (void * p, char c);

void font_set_normal (void);
void font_set_error (void);

void test_print ();

#endif
#endif
4 changes: 4 additions & 0 deletions thorn/include/common/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ extern unsigned int get32 (unsigned long);

extern int get_el (void);

extern void set_pgd (unsigned long pgd);

extern unsigned long get_pgd (void);

#endif /*_ROSE_C_BOOT_H */
6 changes: 4 additions & 2 deletions thorn/include/kernel/arm/sysregs.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
// ESR_EL1, Exception Syndrome Register (EL1). Page 2431 of AArch64-Reference-Manual.
// ***************************************

#define ESR_ELx_EC_SHIFT 26
#define ESR_ELx_EC_SVC64 0x15
#define ESR_ELx_EC_SHIFT 26
#define ESR_ELx_EC_SVC64 0x15
#define ESR_ELx_EC_DABT_LOW 0b100100
#define ESR_ELx_EC_DABT_SAME 0b100101

#endif /*_ROSE_K_A_SYSREGS_H*/
2 changes: 1 addition & 1 deletion thorn/include/kernel/irq.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

void enable_interrupt_controller (void);

void show_invalid_entry_message (int type, ptr_t esr, ptr_t address);
void show_invalid_entry_message (int type, ptr_t esr, ptr_t elr, ptr_t far);

void irq_vector_init (void);

Expand Down
2 changes: 1 addition & 1 deletion thorn/include/kernel/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define PAGE_SIZE (1 << PAGE_SHIFT)
#define SECTION_SIZE (1 << SECTION_SHIFT)

#define LOW_MEMORY (2 * SECTION_SIZE)
#define LOW_MEMORY (1 * SECTION_SIZE)
#define HIGH_MEMORY 0x70000000

#define PAGING_MEMORY (HIGH_MEMORY - LOW_MEMORY)
Expand Down
53 changes: 53 additions & 0 deletions thorn/include/kernel/mmu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#ifndef _ROSE_MMU_H
#define _ROSE_MMU_H

#ifndef __ASSEMBLER__

// Implementation taken from
// https://github.com/bztsrc/raspi3-tutorial/blob/master/10_virtualmemory/mmu.c

#include "common/font.h"
#include "common/logging.h"
#include "common/status_led.h"
#include "kernel/irq.h"
#include "kernel/mini_uart.h"
#include "kernel/peripherals/base.h"
#include "kernel/sched.h"

#define PAGESIZE 4096

// granularity
#define PT_PAGE 0b11// 4k granule
#define PT_BLOCK 0b01// 2M granule
// accessibility
#define PT_KERNEL (0 << 6) // privileged, supervisor EL1 access only
#define PT_USER (1 << 6) // unprivileged, EL0 access allowed
#define PT_RW (0 << 7) // read-write
#define PT_RO (1 << 7) // read-only
#define PT_AF (1 << 10) // accessed flag
#define PT_NX (1UL << 54)// no execute
// shareability
#define PT_OSH (2 << 8)// outter shareable
#define PT_ISH (3 << 8)// inner shareable
// defined in MAIR register
#define PT_MEM (0 << 2)// normal memory
#define PT_DEV (1 << 2)// device MMIO
#define PT_NC (2 << 2)// non-cachable

#define TTBR_CNP 1

#define RAM_IN_GB 8

// get addresses from linker
extern volatile unsigned char _data;
extern volatile unsigned char _end;

static volatile unsigned int __attribute__ ((aligned (16))) buffer[32];

void init_pages ();
void init_mmu (void);

void data_abort_el0 (ptr_t far, ptr_t esr, ptr_t elr);

#endif
#endif//_ROSE_MMU_H
22 changes: 20 additions & 2 deletions thorn/include/kernel/sched.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef _ROSE_K_SCHED_H
#define _ROSE_K_SCHED_H

#include "common/font.h"
#include "common/stddef.h"

#define THREAD_CPU_CONTEXT 0// offset of cpu_context in task_struct
Expand Down Expand Up @@ -39,14 +40,29 @@ struct cpu_context {
unsigned long pc;
};

#define MAX_PROCESS_PAGES 16

struct user_page {
unsigned long phys_addr;
unsigned long virt_addr;
};

struct mm_struct {
unsigned long pgd;
int user_pages_count;
struct user_page user_pages[MAX_PROCESS_PAGES];
int kernel_pages_count;
unsigned long kernel_pages[MAX_PROCESS_PAGES];
};

struct task_struct {
struct cpu_context cpu_context;
long state;
long counter;
long priority;
long preempt_count;
ptr_t stack;
ptr_t flags;
unsigned long flags;
struct mm_struct mm;
};

extern void sched_init (void);
Expand All @@ -65,6 +81,8 @@ extern void cpu_switch_to (struct task_struct * prev, struct task_struct * next)

extern void exit_process (void);

extern void kill_process (void);

extern void task_init (void);

#define INIT_TASK \
Expand Down
10 changes: 10 additions & 0 deletions thorn/src/common/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ void printc (char c) {
}
}

void prints (char const * str) {
while (*str) {
printc (*str++);
}
}

void font_set_normal (void) {
font_fg = font_normal_fg;
}
Expand All @@ -201,3 +207,7 @@ void font_set_error (void) {
void putc_screen (void * p, char c) {
printc (c);
}

void test_print () {
prints ("test\r\n");
}
13 changes: 13 additions & 0 deletions thorn/src/common/printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,19 @@ void tfp_format (void * putp, putcf putf, char * fmt, va_list va) {
unsigned int), 16, (ch == 'P'), bf);
putchw (putp, putf, w, lz, bf);
break;
case 'b':
case 'B':
putf (putp, '0');
putf (putp, 'b');
#ifdef PRINTF_LONG_SUPPORT
if (lng)
uli2a (va_arg (va, unsigned long int), 2, (ch == 'B'), bf);
else
#endif
ui2a (va_arg (va,
unsigned int), 2, (ch == 'B'), bf);
putchw (putp, putf, w, lz, bf);
break;
case '%':
putf (putp, ch);
default:
Expand Down
19 changes: 19 additions & 0 deletions thorn/src/common/utils.S
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "common/font.h"

.globl get_el
get_el:
mrs x0, CurrentEL
Expand All @@ -19,3 +21,20 @@ delay:
subs x0, x0, #1
bne delay
ret

.globl set_pgd
set_pgd:
msr ttbr0_el1, x0
// tlbi vmalle1is // Crashes CPU
dsb ish // ensure completion of TLB invalidation
isb
ret

.globl get_pgd
get_pgd:
mov x1, 0
ldr x0, [x1]
mov x0, 0x1000
msr ttbr0_el1, x0
ldr x0, [x1]
ret
23 changes: 12 additions & 11 deletions thorn/src/kernel/boot.S
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ _start:
b master

init_bss:
adr x0, bss_begin
adr x1, bss_end
adr x0, __bss_start
adr x1, __bss_end
sub x1, x1, x0
bl memzero

adr x0, move_to_el1
mov x1, #0xe0
str x0, [x1]

mov x1, #0xe8
str x0, [x1]

mov x1, #0xf0
str x0, [x1]

sev
// mov x1, #0xe0
// str x0, [x1]
//
// mov x1, #0xe8
// str x0, [x1]
//
// mov x1, #0xf0
// str x0, [x1]
//
// sev

b move_to_el1

Expand Down
52 changes: 33 additions & 19 deletions thorn/src/kernel/entry.S
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "kernel/arm/sysregs.h"
#include "kernel/entry.h"
#include "kernel/sys.h"
#include "kernel/mmu.h"

.macro handle_invalid_entry el, type
kernel_entry \el
mov x0, #\type
mrs x1, esr_el1
mrs x2, elr_el1
mrs x3, far_el1
bl show_invalid_entry_message
b err_hang
.endm
Expand Down Expand Up @@ -91,19 +93,19 @@ vectors:
ventry error_invalid_el1t // Error EL1t

ventry sync_invalid_el1h // Synchronous EL1h
ventry el1_irq // IRQ EL1h
ventry el1_irq // IRQ EL1h
ventry fiq_invalid_el1h // FIQ EL1h
ventry error_invalid_el1h // Error EL1h

ventry el0_sync // Synchronous 64-bit EL0
ventry el0_irq // IRQ 64-bit EL0
ventry el0_sync // Synchronous 64-bit EL0
ventry el0_irq // IRQ 64-bit EL0
ventry fiq_invalid_el0_64 // FIQ 64-bit EL0
ventry error_invalid_el0_64 // Error 64-bit EL0
ventry error_invalid_el0_64 // Error 64-bit EL0

ventry sync_invalid_el0_32 // Synchronous 32-bit EL0
ventry irq_invalid_el0_32 // IRQ 32-bit EL0
ventry fiq_invalid_el0_32 // FIQ 32-bit EL0
ventry error_invalid_el0_32 // Error 32-bit EL0
ventry error_invalid_el0_32 // Error 32-bit EL0

sync_invalid_el1t:
handle_invalid_entry 1, SYNC_INVALID_EL1t
Expand Down Expand Up @@ -156,43 +158,55 @@ el0_irq:

el0_sync:
kernel_entry 0
mrs x25, esr_el1 // read the syndrome register
lsr x24, x25, #ESR_ELx_EC_SHIFT // exception class
cmp x24, #ESR_ELx_EC_SVC64 // SVC in 64-bit state
b.eq el0_svc
mrs x25, esr_el1 // read the syndrome register
lsr x24, x25, #ESR_ELx_EC_SHIFT // exception class
cmp x24, #ESR_ELx_EC_SVC64 // SVC in 64-bit state
b.eq el0_svc
cmp x24, #ESR_ELx_EC_DABT_LOW
b.eq el0_da
handle_invalid_entry 0, SYNC_ERROR

sc_nr .req x25 // number of system calls
scno .req x26 // syscall number
stbl .req x27 // syscall table pointer
sc_nr .req x25 // number of system calls
scno .req x26 // syscall number
stbl .req x27 // syscall table pointer

el0_svc:
adr stbl, sys_call_table // load syscall table pointer
adr stbl, sys_call_table // load syscall table pointer
uxtw scno, w8 // syscall number in w8
mov sc_nr, #__NR_syscalls
bl enable_irq
cmp scno, sc_nr // check upper syscall limit
cmp scno, sc_nr // check upper syscall limit
b.hs ni_sys

ldr x16, [stbl, scno, lsl #3] // address in the syscall table
blr x16 // call sys_* routine
ldr x16, [stbl, scno, lsl #3] // address in the syscall table
blr x16 // call sys_* routine
b ret_from_syscall
ni_sys:
handle_invalid_entry 0, SYSCALL_ERROR
ret_from_syscall:
bl disable_irq
str x0, [sp, #S_X0] // returned x0
str x0, [sp, #S_X0] // returned x0
kernel_exit 0

el0_da:
bl preempt_disable
mrs x0, far_el1
mrs x1, esr_el1
mrs x2, elr_el1
bl data_abort_el0
bl preempt_enable
bl disable_irq
eret

.globl ret_from_fork
ret_from_fork:
bl schedule_tail
cbz x19, ret_to_user // not a kernel thread
cbz x19, ret_to_user // not a kernel thread
mov x0, x20
blr x19
ret_to_user:
bl disable_irq
kernel_exit 0

.globl err_hang
err_hang: b err_hang
err_hang: b err_hang
Loading