Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add initial process, thread and scheduler #46

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ build/
/.idea/
!/.idea/runConfigurations
/.vscode/
!/.idea/settings.json
*~
*.swp
[._]*.s[a-w][a-z]
Expand Down Expand Up @@ -60,4 +61,4 @@ compile_commands.json
# Misc
NOTES
/server.PID

compile_commands.json
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"cwd": "${workspaceRoot}",
// "preLaunchTask": "run_debug",
"valuesFormatting": "prettyPrinters"
},
}
]
}
}
24 changes: 0 additions & 24 deletions kernel/src/common/include/interrupt.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,6 @@

#define BRANCH_INSTRUCTION 0xe59ff018 // ldr pc, pc+offset (where offset is 0x20 bytes)

// System Call Types
// TODO: maybe make enum?
#define SYSCALL_CREATE 0
#define SYSCALL_SWITCH 1
#define SYSCALL_DELETE 2
#define SYSCALL_OPEN 3
#define SYSCALL_READ 4
#define SYSCALL_WRITE 5
#define SYSCALL_CLOSE 6
#define SYSCALL_SET_PERM 7
#define SYSCALL_MEM_MAP 8
#define SYSCALL_SEEK 9
#define SYSCALL_MKDIR 10
#define SYSCALL_COPY 11
#define SYSCALL_LS 12
#define SYSCALL_MALLOC 13
#define SYSCALL_ALIGNED_ALLOC 14
#define SYSCALL_FREE 15
#define SYSCALL_PRINTF 16
#define SYSCALL_DUMMY 99
#define SYSCALL_EXIT 100
#define SYSCALL_WRITEV 101
#define SYSCALL_PAUSE 102

void init_vector_table(void);

// vector table handlers, should be loaded at 0x00 in this order!
Expand Down
2 changes: 2 additions & 0 deletions kernel/src/common/include/kernel_programs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
void kernel_one();
void kernel_two();
86 changes: 6 additions & 80 deletions kernel/src/common/interrupt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <interrupt.h>
#include <mmio.h>
#include <stdio.h>
#include <syscallhandler.h>
#include <vm2.h>

/* copy vector table from wherever QEMU loads the kernel to 0x00 */
Expand Down Expand Up @@ -77,15 +78,16 @@ void __attribute__((interrupt("UNDEF"))) undef_instruction_handler() {
}

long __attribute__((interrupt("SWI"))) software_interrupt_handler(void) {
int callNumber = 0, r0 = 0, r1 = 0, r2 = 0, r3 = 0;
size_t callNumber = 0, r0 = 0, r1 = 0, r2 = 0, r3 = 0;

asm volatile("MOV %0, r7" : "=r"(callNumber)::);
asm volatile("MOV %0, r0" : "=r"(r0)::);
asm volatile("MOV %0, r1" : "=r"(r1)::);
asm volatile("MOV %0, r2" : "=r"(r2)::);
asm volatile("MOV %0, r3" : "=r"(r3)::);

kprintf("SOFTWARE INTERRUPT HANDLER\n");
DEBUG("SYSCALL");


// Print out syscall # for debug purposes
kprintf("Syscall #: ");
Expand All @@ -96,85 +98,9 @@ long __attribute__((interrupt("SWI"))) software_interrupt_handler(void) {
kprintf("arg3=%d\n", r3);
kprintf("\n");

// System Call Handler
switch (callNumber) {
case SYSCALL_EXIT:
// TODO: remove current process from scheduler
for (;;)
;
break;
case SYSCALL_DUMMY:
return 0L;
break;
handle_syscall(callNumber, r0, r1, r2, r3);

// NOTE: All FS syscalls have been *DISABLED* until the filesystem works again.
case SYSCALL_CREATE:
kprintf("Create system call called!\n");
return -1;

// return (long) kcreate((char*) r0, r1, 0);
case SYSCALL_DELETE:
kprintf("Delete system call called!\n");
return -1;

// return (long) kdelete((char*) r0, 1);
case SYSCALL_OPEN:
kprintf("Open system call called!\n");
return -1;

// return (long) kopen((char*) r0, r1);
case SYSCALL_MKDIR:
kprintf("Mkdir system call called!\n");
return -1;

// return (long) kcreate((char*) r0, 'w', 1);
case SYSCALL_READ:
kprintf("Read system call called!\n");
return -1;

// return (long) kread(r0, (void*) r1, r2);
case SYSCALL_WRITE:
kprintf("Write system call called!\n");
return -1;

// return (long) kwrite(r0, (void*) r1, r2);
case SYSCALL_CLOSE:
kprintf("Close system call called!\n");
return -1;

// return (long) kclose(r0);
case SYSCALL_SEEK:
kprintf("Seek system call called!\n");
return -1;

// return (long) kseek(r0, r1);
case SYSCALL_COPY:
kprintf("Copy system call called!\n");
return -1;

// return (long) kcopy((char*) r0, (char*) r1, r2);
case SYSCALL_LS:
kprintf("Ls system call called!\n");
return -1;
// return (long) kls((char*) r0);
case SYSCALL_SET_PERM:
kprintf("Set permission system call called!\n");
kprintf("Yet to be implemented\n");
return -1;
case SYSCALL_MEM_MAP:
kprintf("Memory map system call called!\n");
kprintf("Yet to be implemented\n");
return -1;

case SYSCALL_PRINTF:
kprintf("Printf system call called!\n");

kprintf((const char *)r0);
return 0L;
default:
kprintf("That wasn't a syscall you knob!\n");
return -1L;
}
return 0;
}

void __attribute__((interrupt("ABORT"))) prefetch_abort_handler(void) {
Expand Down
29 changes: 29 additions & 0 deletions kernel/src/common/kernel_programs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>

void kernel_one() {
/** asm volatile(
"mov %r7, $4 \n" \
"swi $0 \n"
); */
kprintf("Starting kernel program one.\n");

for (size_t i = 0; i < 100; i++) {
kprintf("Hello #%i from program one.\n", i);
}

kprintf("Goodbye from program one.\n");
}

void kernel_two() {
asm volatile(
"mov %r7, $4 \n" \
"swi $0 \n"
);
kprintf("Starting kernel program two.\n");

for (size_t i = 0; i < 100; i++) {
kprintf("Hello #%i from program two.\n", i);
}

kprintf("Goodbye from program two.\n");
}
13 changes: 12 additions & 1 deletion kernel/src/common/start.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#include <hardwareinfo.h>
#include <interrupt.h>
#include <klibc.h>
#include <kernel_programs.h>
#include <mem_alloc.h>
#include <stdint.h>
#include <test.h>
#include <vm2.h>
#include <test.h>
#include <scheduler.h>

/// Entrypoint for the C part of the kernel.
/// This function is called by the assembly located in [startup.s].
Expand Down Expand Up @@ -50,6 +52,7 @@ void start(uint32_t * p_bootargs) {
// Call the chipset again to do any initialization after enabling interrupts and the heap.
chipset.late_init();

Scheduler *scheduler = create_scheduler();

#ifndef ENABLE_TESTS
// argparse_process(p_bootargs);
Expand All @@ -62,6 +65,14 @@ void start(uint32_t * p_bootargs) {
#endif


Process *process1 = create_process(kernel_one, NULL);
Process *process2 = create_process(kernel_two, NULL);
add_process_to_scheduler(scheduler, process1);
add_process_to_scheduler(scheduler, process2);

enable_scheduler(scheduler);
free_scheduler(scheduler);

// TODO:
// * Mount vfs
// * Load initramfs into tmpfs
Expand Down
3 changes: 2 additions & 1 deletion kernel/src/drivers/chipset/bcm2836/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <chipset.h>
#include <interrupt.h>
#include <limits.h>
#include <klibc.h>

/*
// raspberry pi zero, 1, b+ etc
Expand Down Expand Up @@ -106,7 +107,7 @@ void bcm2836_uart_putc(char c, int uartchannel) {
case 3:
return uart_write_byte(BCM2836_UART3_ADDRESS, c);
default:
__unreachable();
panic();
}
}

Expand Down
50 changes: 50 additions & 0 deletions kernel/src/process/asm_helper.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.text
.global switch_context

switch_context:
; Set the CPSR register.
dsluijk marked this conversation as resolved.
Show resolved Hide resolved
ldr r1, [r0, #16]
msr cpsr, r1
; Load the stack pointer.
ldr sp, [r0, #52]
; Push all other registers on the stack.
ldr r1, [r0, #60]
push {r1}
ldr r1, [r0, #56]
push {r1}
ldr r1, [r0, #48]
push {r1}
ldr r1, [r0, #44]
push {r1}
ldr r1, [r0, #40]
push {r1}
ldr r1, [r0, #36]
push {r1}
ldr r1, [r0, #32]
push {r1}
ldr r1, [r0, #28]
push {r1}
ldr r1, [r0, #24]
push {r1}
ldr r1, [r0, #20]
push {r1}
ldr r1, [r0, #16]
push {r1}
ldr r1, [r0, #12]
push {r1}
ldr r1, [r0, #8]
push {r1}
ldr r1, [r0, #4]
push {r1}
ldr r1, [r0, #0]
push {r1}

; TODO: Enter user space
; cps #16
; Or:
; msr CPSR_c, #0x10

; Load all register states.
; Loading the PC register effectively jumps into the user program
pop {r0-r12}
pop {r14-pc}
8 changes: 8 additions & 0 deletions kernel/src/process/include/asm_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef COURSE_OS_ASM_HELPER_H
#define COURSE_OS_ASM_HELPER_H

#include "./registers.h"

void switch_context(Registers *registers);

#endif //COURSE_OS_ASM_HELPER_H
22 changes: 22 additions & 0 deletions kernel/src/process/include/process.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef COURSE_OS_PROCESS_H
#define COURSE_OS_PROCESS_H

#include <vp_array_list.h>
#include <vas2.h>

#define MAX_PROCESS_PRIORITY 20
#define DEFAULT_PROCESS_PRIORITY 10

typedef struct Process {
uint8_t priority;
size_t pid;
uint8_t exit_code;
struct vas2* vas;
VPArrayList *threads;
struct Process *parent;
} Process;

Process *create_process(void *entry, Process *parent);
void free_process(Process *process);

#endif //COURSE_OS_PROCESS_H
50 changes: 50 additions & 0 deletions kernel/src/process/include/registers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef COURSE_OS_REGISTERS_H
#define COURSE_OS_REGISTERS_H

#include <stdint.h>

typedef struct Registers {
size_t R0;
size_t R1;
size_t R2;
size_t R3;
size_t R4;
size_t R5;
size_t R6;
union {
size_t R7;
size_t WR;
};
size_t R8;
union {
size_t R9;
size_t SB;
};
union {
size_t R10;
size_t SL;
};
union {
size_t R11;
size_t FP;
};
union {
size_t R12;
size_t IP;
};
union {
size_t R13;
size_t SP;
};
union {
size_t R14;
size_t LR;
};
union {
size_t R15;
size_t PC;
};
size_t CPSR;
} Registers;

#endif //COURSE_OS_REGISTERS_H
Loading