Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
04900fbd74
|
|||
| 935564c4b2 | |||
|
c00a247ead
|
|||
|
ccb6ca89f1
|
@@ -23,6 +23,8 @@ LD := x86_64-elf-ld
|
||||
$(ELFFILE): $(BUILDDIR) $(OBJFILES)
|
||||
nasm -f bin user/hello.S -o $(BUILDDIR)/hello
|
||||
nasm -f bin user/pedicel.S -o $(BUILDDIR)/pedicel
|
||||
tar cvf $(BUILDDIR)/initfs.tar -C $(BUILDDIR) hello pedicel
|
||||
|
||||
nasm -f elf64 src/arch/x86/idt.S -o $(BUILDDIR)/idt_stub.o
|
||||
$(LD) -o $(ELFFILE) -T linker.ld $(OBJFILES) $(BUILDDIR)/idt_stub.o
|
||||
# Get the symbols for debugging
|
||||
@@ -43,14 +45,15 @@ limine/limine:
|
||||
git clone https://github.com/limine-bootloader/limine.git --branch=v9.x-binary --depth=1
|
||||
$(MAKE) -C limine
|
||||
|
||||
initfs:
|
||||
|
||||
build-iso: limine/limine $(ELFFILE)
|
||||
rm -rf iso_root
|
||||
mkdir -p iso_root/boot
|
||||
cp -v $(ELFFILE) iso_root/boot
|
||||
mkdir -p iso_root/boot/limine
|
||||
cp -v limine.conf iso_root/boot/limine
|
||||
cp $(BUILDDIR)/hello iso_root/boot/
|
||||
cp $(BUILDDIR)/pedicel iso_root/boot/
|
||||
cp $(BUILDDIR)/initfs.tar iso_root/boot/
|
||||
mkdir -p iso_root/EFI/BOOT
|
||||
cp -v limine/limine-bios.sys limine/limine-bios-cd.bin limine/limine-uefi-cd.bin iso_root/boot/limine/
|
||||
cp -v limine/BOOTX64.EFI iso_root/EFI/BOOT/
|
||||
|
||||
@@ -63,4 +63,7 @@
|
||||
/* ssp */
|
||||
#define STACK_CHK_GUARD 0x7ABA5C007ABA5C00
|
||||
|
||||
/* fs */
|
||||
#define FDT_MAX 8 // Maximum amount of file descriptors per process
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* @author xamidev <xamidev@riseup.net>
|
||||
* @brief PS/2 Keyboard driver
|
||||
* @license GPL-3.0-only
|
||||
*/
|
||||
|
||||
#ifndef INITFS_H
|
||||
#define INITFS_H
|
||||
|
||||
#include <limine.h>
|
||||
|
||||
int initfs_init(struct limine_file* tar_file);
|
||||
int tar_lookup(unsigned char* archive, char* filename, char** out);
|
||||
int tar_read(char* filename, char** buf);
|
||||
int tar_exists(const char* filename);
|
||||
|
||||
#endif
|
||||
+8
-2
@@ -8,9 +8,13 @@
|
||||
#define KERNEL_H
|
||||
|
||||
#include "limine.h"
|
||||
|
||||
enum ErrorCodes {
|
||||
ENOMEM,
|
||||
EIO
|
||||
ENOMEM, // No memory
|
||||
EIO, // Input/output error
|
||||
ENOENT, // No entry
|
||||
EBADFD, // Bad file descriptor
|
||||
EMFILE // Too many open files
|
||||
};
|
||||
|
||||
#define CLEAR_INTERRUPTS __asm__ volatile("cli")
|
||||
@@ -46,6 +50,8 @@ void debug_stack_trace(unsigned int max_frames);
|
||||
const char* debug_find_symbol(uintptr_t rip, uintptr_t* offset);
|
||||
void boot_mem_display(void);
|
||||
|
||||
int loader_load_raw();
|
||||
|
||||
#define assert(check) do { if(!(check)) hcf(); } while(0)
|
||||
|
||||
struct boot_context {
|
||||
|
||||
+13
-1
@@ -11,6 +11,7 @@
|
||||
#include <config.h>
|
||||
#include <stdint.h>
|
||||
#include <limine.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef enum {
|
||||
READY,
|
||||
@@ -18,6 +19,13 @@ typedef enum {
|
||||
DEAD
|
||||
} status_t;
|
||||
|
||||
struct fd {
|
||||
int fd;
|
||||
char filename[PROCESS_NAME_MAX]; // File opened
|
||||
uint64_t cursor; // Cursor position in file
|
||||
bool open;
|
||||
};
|
||||
|
||||
struct process {
|
||||
size_t pid;
|
||||
char name[PROCESS_NAME_MAX];
|
||||
@@ -26,6 +34,10 @@ struct process {
|
||||
struct cpu_status* context;
|
||||
void* root_page_table; // Process PML4 (should contain kernel PML4 in higher half [256-511]
|
||||
void* kernel_stack; // Used for interrupts (syscall: int 0x80), defines the TSS RSP0
|
||||
|
||||
struct fd fdt[FDT_MAX]; // File Descriptor Table
|
||||
size_t next_free_fd;
|
||||
|
||||
struct process* next;
|
||||
};
|
||||
|
||||
@@ -38,6 +50,6 @@ void process_exit(void);
|
||||
|
||||
void process_display_list(struct process* processes_list);
|
||||
|
||||
void process_create_user(struct limine_file* file, char* name);
|
||||
void process_create_user_raw(char* file, int size, char* name);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -13,5 +13,6 @@ char *strcpy(char *dest, const char *src);
|
||||
char *strcat(char *dest, const char *src);
|
||||
void strncpy(char* dst, const char* src, size_t n);
|
||||
int strncmp(const char* s1, const char* s2, size_t n);
|
||||
size_t strlen(const char* str);
|
||||
|
||||
#endif
|
||||
+1
-2
@@ -6,5 +6,4 @@ interface_branding: Welcome to the PepperOS disk!
|
||||
|
||||
comment: Default configuration (warning: spicy)
|
||||
path: boot():/boot/pepperk
|
||||
module_path: boot():/boot/hello
|
||||
module_path: boot():/boot/pedicel
|
||||
module_path: boot():/boot/initfs.tar
|
||||
+77
-2
@@ -4,15 +4,84 @@
|
||||
* @license GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "sched/scheduler.h"
|
||||
#include <arch/x86.h>
|
||||
#include <kernel.h>
|
||||
#include <stddef.h>
|
||||
#include <io/term/term.h>
|
||||
#include <sched/process.h>
|
||||
#include <io/kbd/ps2.h>
|
||||
#include <fs/initfs.h>
|
||||
#include <string/string.h>
|
||||
|
||||
extern struct process* current_process;
|
||||
|
||||
// Return fd on success, -errno on error
|
||||
int sys_open(const char* filename, int flags)
|
||||
{
|
||||
if (tar_exists(filename) < 0) {
|
||||
return -ENOENT; // file doesn't exist..
|
||||
}
|
||||
// file exists here!
|
||||
if (current_process->next_free_fd >= FDT_MAX) {
|
||||
return -EMFILE;
|
||||
}
|
||||
int fd = current_process->next_free_fd++;
|
||||
current_process->fdt[fd].fd = fd;
|
||||
current_process->fdt[fd].open = true;
|
||||
current_process->fdt[fd].cursor = 0;
|
||||
strncpy(current_process->fdt[fd].filename, filename, PROCESS_NAME_MAX - 1);
|
||||
return fd;
|
||||
}
|
||||
|
||||
// Return 0 on success, -EBADFD if invalid FD
|
||||
int sys_close(int fd)
|
||||
{
|
||||
if (fd < 0 || fd >= FDT_MAX) {
|
||||
return -EBADFD;
|
||||
}
|
||||
|
||||
if (!current_process->fdt[fd].open) {
|
||||
return -EBADFD; // FD not opened in the first place
|
||||
}
|
||||
|
||||
current_process->fdt[fd].open = false;
|
||||
current_process->fdt[fd].filename[0] = '\0';
|
||||
current_process->fdt[fd].cursor = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Should return the number of bytes read
|
||||
int sys_read(unsigned int fd, char* buf, size_t count)
|
||||
{
|
||||
size_t i;
|
||||
switch (fd) {
|
||||
case 0: //read from stdin (keyboard)
|
||||
for (i=0; i<count; i++) {
|
||||
buf[i] = keyboard_getchar();
|
||||
}
|
||||
return i;
|
||||
case 1: // from stdout
|
||||
case 2: // from stderr
|
||||
return -EBADFD;
|
||||
default: // from an open file?
|
||||
if (current_process->fdt[fd].open == false) {
|
||||
return -EBADFD; // File descriptor wasn't open
|
||||
}
|
||||
// Here fd refers to a valid opened file..
|
||||
int sz = tar_read(current_process->fdt[fd].filename,&buf);
|
||||
if (sz == 0) {
|
||||
return -ENOENT;
|
||||
} else {
|
||||
return sz;
|
||||
}
|
||||
}
|
||||
|
||||
return -EBADFD;
|
||||
}
|
||||
|
||||
// TODO: Should have a return value: number of bytes written on success, -1 on error (errno set)
|
||||
void sys_write(unsigned int fd, const char* buf, size_t count)
|
||||
{
|
||||
switch (fd) {
|
||||
@@ -61,17 +130,23 @@ struct cpu_status* syscall_handler(struct cpu_status* regs)
|
||||
switch (regs->rax)
|
||||
{
|
||||
case 0: //sys_read
|
||||
regs->rax = sys_read(regs->rdi, (char*)regs->rsi, regs->rdx);
|
||||
break;
|
||||
case 1: //sys_write
|
||||
sys_write(regs->rdi, (char*)regs->rsi, regs->rdx);
|
||||
break;
|
||||
case 2:
|
||||
regs->rax = sys_open((const char*)regs->rdi, regs->rsi);
|
||||
break;
|
||||
case 3:
|
||||
regs->rax = sys_close(regs->rdi);
|
||||
break;
|
||||
case 60: //sys_exit
|
||||
sys_exit(regs->rdi);
|
||||
break;
|
||||
default:
|
||||
default: // bad syscall
|
||||
regs->rax = 0xbad515ca11;
|
||||
break;
|
||||
}
|
||||
|
||||
return regs;
|
||||
}
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* @author xamidev <xamidev@riseup.net>
|
||||
* @brief Initial TAR filesystem (read-only)
|
||||
* @license GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include <sched/process.h>
|
||||
#include <limine.h>
|
||||
#include <fs/initfs.h>
|
||||
#include <kernel.h>
|
||||
#include <mem/utils.h>
|
||||
#include <string/string.h>
|
||||
|
||||
void* archive_start_addr;
|
||||
uint64_t archive_size;
|
||||
|
||||
/*
|
||||
* tar_oct2bin - convert octal size string to an integer
|
||||
* @str: octal size string
|
||||
* @size: size of string
|
||||
*
|
||||
* Return:
|
||||
* $n - file size as an integer
|
||||
*/
|
||||
int tar_oct2bin(unsigned char* str, int size)
|
||||
{
|
||||
int n = 0;
|
||||
unsigned char* c = str;
|
||||
while (size-- > 0) {
|
||||
n *= 8;
|
||||
n += *c - '0';
|
||||
c++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/*
|
||||
* tar_lookup - lookup a file in the TAR file
|
||||
* @archive: pointer to beginning of the archive
|
||||
* @filename: file to lookup (absolute path)
|
||||
* @out: where to store file data if found
|
||||
*
|
||||
* Return:
|
||||
* $filesize - size of the file, if found
|
||||
* $0 - file not found
|
||||
*/
|
||||
int tar_lookup(unsigned char* archive, char* filename, char** out)
|
||||
{
|
||||
unsigned char *ptr = archive;
|
||||
|
||||
while (!memcmp(ptr + 257, "ustar", 5)) {
|
||||
int filesize = tar_oct2bin(ptr + 0x7c, 11);
|
||||
if (!memcmp(ptr, filename, strlen(filename) + 1)) {
|
||||
*out = (char*)(ptr + 512);
|
||||
return filesize;
|
||||
}
|
||||
ptr += (((filesize + 511) / 512) + 1) * 512;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* tar_read - read a file in the TAR file
|
||||
* @filename: file to read (absolute path)
|
||||
* @out: out buffer (if file is found)
|
||||
*
|
||||
* Return:
|
||||
* $filesize - size of the file, if found
|
||||
* $0 - file not found
|
||||
*/
|
||||
int tar_read(char* filename, char** buf)
|
||||
{
|
||||
return tar_lookup(archive_start_addr, filename, buf);
|
||||
}
|
||||
|
||||
/*
|
||||
* tar_exists - check if a file exists in the TAR archive
|
||||
* @filename: file to check (absolute path)
|
||||
*
|
||||
* Return:
|
||||
* $filesize - size of the file, if found
|
||||
* $-ENOENT - file not found
|
||||
*/
|
||||
int tar_exists(const char* filename)
|
||||
{
|
||||
unsigned char* ptr = archive_start_addr;
|
||||
|
||||
while (!memcmp(ptr + 257, "ustar", 5)) {
|
||||
int filesize = tar_oct2bin(ptr + 0x7c, 11);
|
||||
if (!memcmp(ptr, filename, strlen(filename) + 1)) {
|
||||
return filesize;
|
||||
}
|
||||
ptr += (((filesize + 511) / 512) + 1) * 512;
|
||||
}
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
/*
|
||||
* initfs_init - initialize the TAR initial filesystem
|
||||
* @tar_file: pointer to the Limine-loaded archive
|
||||
*
|
||||
* Return:
|
||||
* $0 - on success
|
||||
*/
|
||||
int initfs_init(struct limine_file* tar_file)
|
||||
{
|
||||
archive_start_addr = tar_file->address;
|
||||
archive_size = tar_file->size;
|
||||
|
||||
DEBUG("Loaded TAR initial filesystem (initfs.tar)");
|
||||
return 0;
|
||||
}
|
||||
+7
-1
@@ -51,7 +51,8 @@ void pedicel_main(void* arg)
|
||||
"pf - trigger a page fault\r\n"
|
||||
"now - get current date\r\n"
|
||||
"smash - smash the stack\r\n"
|
||||
"mem - get used heap info\r\n");
|
||||
"mem - get used heap info\r\n"
|
||||
"load - load an user executable\r\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -96,6 +97,11 @@ void pedicel_main(void* arg)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (strncmp(input_buf, "load", 4) == 0) {
|
||||
loader_load_raw();
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("%s: command not found\r\n", input_buf);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* @author xamidev <xamidev@riseup.net>
|
||||
* @brief Executable loader
|
||||
* @license GPL-3.0-only
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <fs/initfs.h>
|
||||
#include <kernel.h>
|
||||
#include <sched/process.h>
|
||||
#include <io/kbd/ps2.h>
|
||||
#include <string/string.h>
|
||||
|
||||
extern void* archive_start_addr;
|
||||
|
||||
int loader_load_raw()
|
||||
{
|
||||
char input_buf[PEDICEL_INPUT_SIZE] = {0};
|
||||
do {
|
||||
printf("file> ");
|
||||
keyboard_getline(input_buf, PEDICEL_INPUT_SIZE);
|
||||
} while (strncmp(input_buf, "", 1) == 0);
|
||||
|
||||
char* data = NULL;
|
||||
int sz = tar_lookup(archive_start_addr, input_buf,&data);
|
||||
if (sz > 0) {
|
||||
process_create_user_raw(data, sz, input_buf);
|
||||
return 0; // TODO: should return something else on error
|
||||
}
|
||||
printf("Couldn't load file '%s'\r\n", input_buf);
|
||||
return 1;
|
||||
}
|
||||
+5
-16
@@ -25,6 +25,7 @@
|
||||
#include <io/term/flanterm_backends/fb.h>
|
||||
#include <arch/x86.h>
|
||||
#include <boot/boot.h>
|
||||
#include <fs/initfs.h>
|
||||
|
||||
// Limine version used
|
||||
__attribute__((used, section(".limine_requests")))
|
||||
@@ -77,14 +78,6 @@ void idle_main(void* arg)
|
||||
}
|
||||
}
|
||||
|
||||
void thing_main(void* arg)
|
||||
{
|
||||
printf("What's your name, pal? ");
|
||||
char name[10];
|
||||
keyboard_getline(name, 10);
|
||||
printf("\r\n{%s} is such a nice name!\r\n", name);
|
||||
}
|
||||
|
||||
extern uintptr_t kheap_start;
|
||||
|
||||
/*
|
||||
@@ -120,16 +113,12 @@ void kmain()
|
||||
idle_proc = process_create("idle", (void*)idle_main, 0);
|
||||
|
||||
if (!boot_ctx.module) {
|
||||
panic(NULL, "could not load 'hello' executable :(");
|
||||
panic(NULL, "could not load initfs.tar :(");
|
||||
}
|
||||
if (boot_ctx.module->module_count == 1) {
|
||||
initfs_init(boot_ctx.module->modules[0]);
|
||||
}
|
||||
if (boot_ctx.module->module_count == 2) {
|
||||
file = boot_ctx.module->modules[0];
|
||||
DEBUG("file: addr=%p size=%u", file->address, file->size);
|
||||
process_create_user(file, "hello");
|
||||
|
||||
file = boot_ctx.module->modules[1];
|
||||
process_create_user(file, "pedicel");
|
||||
}
|
||||
process_create("kshell", (void*)pedicel_main, 0);
|
||||
scheduler_init();
|
||||
|
||||
|
||||
+31
-14
@@ -222,32 +222,31 @@ void process_jump_to_user(uintptr_t stack_top, uintptr_t user_code)
|
||||
" :: "r"(stack_top), "r"(user_code));
|
||||
}
|
||||
|
||||
// Kernel stack used for interrupts from userland process.
|
||||
// Should be set in TSS.RSP0 when switching to userland process.
|
||||
uint8_t interrupt_stack[0x8000];
|
||||
|
||||
extern struct tss tss;
|
||||
|
||||
/*
|
||||
* process_create_user - Create a new user process
|
||||
* @file: pointer to Limine file structure
|
||||
* process_create_user_raw - Create a new user process from raw binary
|
||||
* @file: pointer to beginning of binary
|
||||
* @size: size of the binary
|
||||
* @name: name for the new process
|
||||
*
|
||||
* This function takes a loaded Limine executable
|
||||
* module, and maps its code, a user stack, sets the
|
||||
* TSS RSP0 for interrupts, and finally jumps to the
|
||||
* user code.
|
||||
* This function takes an executable loaded in memory
|
||||
* and maps its code, a user stack, sets the TSS RSP0
|
||||
* for interrupts, and finally jumps to the user code.
|
||||
*/
|
||||
void process_create_user(struct limine_file* file, char* name)
|
||||
void process_create_user_raw(char* file, int size, char* name)
|
||||
{
|
||||
// Need to refactor this mess
|
||||
CLEAR_INTERRUPTS;
|
||||
struct process* proc = (struct process*)kmalloc(sizeof(struct process));
|
||||
struct cpu_status* ctx = (struct cpu_status*)kmalloc(sizeof(struct cpu_status));
|
||||
|
||||
if (!proc || !ctx) panic(NULL, "out of memory while creating user process");
|
||||
|
||||
memset(proc, 0, sizeof(struct process));
|
||||
memset(ctx, 0, sizeof(struct cpu_status));
|
||||
|
||||
strncpy(proc->name, name, PROCESS_NAME_MAX);
|
||||
memset(ctx, 0, sizeof(struct cpu_status)); // set GP registers to zero
|
||||
proc->pid = next_free_pid++;
|
||||
proc->status = READY;
|
||||
proc->next = 0;
|
||||
@@ -256,8 +255,26 @@ void process_create_user(struct limine_file* file, char* name)
|
||||
proc->context->iret_cs = USER_CODE_SEGMENT | 3;
|
||||
proc->context->iret_flags = 0x202; // Interrupt Flag set
|
||||
|
||||
void* exec_addr = file->address;
|
||||
uint64_t exec_size = file->size;
|
||||
/* Set basic entries for the process's File Descriptor Table */
|
||||
proc->fdt[0].fd = 0;
|
||||
proc->fdt[0].open = true;
|
||||
proc->fdt[0].cursor = 0;
|
||||
strncpy(proc->fdt[0].filename, "stdin", PROCESS_NAME_MAX - 1);
|
||||
|
||||
proc->fdt[1].fd = 1;
|
||||
proc->fdt[1].open = true;
|
||||
proc->fdt[1].cursor = 0;
|
||||
strncpy(proc->fdt[1].filename, "stdout", PROCESS_NAME_MAX - 1);
|
||||
|
||||
proc->fdt[2].fd = 2;
|
||||
proc->fdt[2].open = true;
|
||||
proc->fdt[2].cursor = 0;
|
||||
strncpy(proc->fdt[2].filename, "stderr", PROCESS_NAME_MAX - 1);
|
||||
|
||||
proc->next_free_fd = 3; // file descriptors are also bump-allocated
|
||||
|
||||
void* exec_addr = (void*)file;
|
||||
uint64_t exec_size = size;
|
||||
|
||||
uint64_t* user_pml4 = vmm_create_address_space();
|
||||
if (!user_pml4) panic(NULL, "failed to create user address space");
|
||||
|
||||
@@ -99,3 +99,11 @@ int strncmp(const char* s1, const char* s2, size_t n)
|
||||
return ( *(unsigned char *)s1 - *(unsigned char *)s2 );
|
||||
}
|
||||
}
|
||||
|
||||
// BSD implementation
|
||||
size_t strlen(const char* str)
|
||||
{
|
||||
const char* s;
|
||||
for (s = str; *s; ++s);
|
||||
return (s - str);
|
||||
}
|
||||
Reference in New Issue
Block a user