Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
04900fbd74
|
|||
| 935564c4b2 |
@@ -63,4 +63,7 @@
|
|||||||
/* ssp */
|
/* ssp */
|
||||||
#define STACK_CHK_GUARD 0x7ABA5C007ABA5C00
|
#define STACK_CHK_GUARD 0x7ABA5C007ABA5C00
|
||||||
|
|
||||||
|
/* fs */
|
||||||
|
#define FDT_MAX 8 // Maximum amount of file descriptors per process
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -11,5 +11,7 @@
|
|||||||
|
|
||||||
int initfs_init(struct limine_file* tar_file);
|
int initfs_init(struct limine_file* tar_file);
|
||||||
int tar_lookup(unsigned char* archive, char* filename, char** out);
|
int tar_lookup(unsigned char* archive, char* filename, char** out);
|
||||||
|
int tar_read(char* filename, char** buf);
|
||||||
|
int tar_exists(const char* filename);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+6
-3
@@ -8,10 +8,13 @@
|
|||||||
#define KERNEL_H
|
#define KERNEL_H
|
||||||
|
|
||||||
#include "limine.h"
|
#include "limine.h"
|
||||||
|
|
||||||
enum ErrorCodes {
|
enum ErrorCodes {
|
||||||
ENOMEM,
|
ENOMEM, // No memory
|
||||||
EIO,
|
EIO, // Input/output error
|
||||||
ENOENT
|
ENOENT, // No entry
|
||||||
|
EBADFD, // Bad file descriptor
|
||||||
|
EMFILE // Too many open files
|
||||||
};
|
};
|
||||||
|
|
||||||
#define CLEAR_INTERRUPTS __asm__ volatile("cli")
|
#define CLEAR_INTERRUPTS __asm__ volatile("cli")
|
||||||
|
|||||||
+12
-1
@@ -11,6 +11,7 @@
|
|||||||
#include <config.h>
|
#include <config.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <limine.h>
|
#include <limine.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
READY,
|
READY,
|
||||||
@@ -18,6 +19,13 @@ typedef enum {
|
|||||||
DEAD
|
DEAD
|
||||||
} status_t;
|
} status_t;
|
||||||
|
|
||||||
|
struct fd {
|
||||||
|
int fd;
|
||||||
|
char filename[PROCESS_NAME_MAX]; // File opened
|
||||||
|
uint64_t cursor; // Cursor position in file
|
||||||
|
bool open;
|
||||||
|
};
|
||||||
|
|
||||||
struct process {
|
struct process {
|
||||||
size_t pid;
|
size_t pid;
|
||||||
char name[PROCESS_NAME_MAX];
|
char name[PROCESS_NAME_MAX];
|
||||||
@@ -26,6 +34,10 @@ struct process {
|
|||||||
struct cpu_status* context;
|
struct cpu_status* context;
|
||||||
void* root_page_table; // Process PML4 (should contain kernel PML4 in higher half [256-511]
|
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
|
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;
|
struct process* next;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -38,7 +50,6 @@ void process_exit(void);
|
|||||||
|
|
||||||
void process_display_list(struct process* processes_list);
|
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);
|
void process_create_user_raw(char* file, int size, char* name);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+77
-2
@@ -4,15 +4,84 @@
|
|||||||
* @license GPL-3.0-only
|
* @license GPL-3.0-only
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
#include "sched/scheduler.h"
|
#include "sched/scheduler.h"
|
||||||
#include <arch/x86.h>
|
#include <arch/x86.h>
|
||||||
#include <kernel.h>
|
#include <kernel.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <io/term/term.h>
|
#include <io/term/term.h>
|
||||||
#include <sched/process.h>
|
#include <sched/process.h>
|
||||||
|
#include <io/kbd/ps2.h>
|
||||||
|
#include <fs/initfs.h>
|
||||||
|
#include <string/string.h>
|
||||||
|
|
||||||
extern struct process* current_process;
|
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)
|
void sys_write(unsigned int fd, const char* buf, size_t count)
|
||||||
{
|
{
|
||||||
switch (fd) {
|
switch (fd) {
|
||||||
@@ -61,17 +130,23 @@ struct cpu_status* syscall_handler(struct cpu_status* regs)
|
|||||||
switch (regs->rax)
|
switch (regs->rax)
|
||||||
{
|
{
|
||||||
case 0: //sys_read
|
case 0: //sys_read
|
||||||
|
regs->rax = sys_read(regs->rdi, (char*)regs->rsi, regs->rdx);
|
||||||
break;
|
break;
|
||||||
case 1: //sys_write
|
case 1: //sys_write
|
||||||
sys_write(regs->rdi, (char*)regs->rsi, regs->rdx);
|
sys_write(regs->rdi, (char*)regs->rsi, regs->rdx);
|
||||||
break;
|
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
|
case 60: //sys_exit
|
||||||
sys_exit(regs->rdi);
|
sys_exit(regs->rdi);
|
||||||
break;
|
break;
|
||||||
default:
|
default: // bad syscall
|
||||||
regs->rax = 0xbad515ca11;
|
regs->rax = 0xbad515ca11;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return regs;
|
return regs;
|
||||||
}
|
}
|
||||||
@@ -59,6 +59,42 @@ int tar_lookup(unsigned char* archive, char* filename, char** out)
|
|||||||
return 0;
|
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
|
* initfs_init - initialize the TAR initial filesystem
|
||||||
* @tar_file: pointer to the Limine-loaded archive
|
* @tar_file: pointer to the Limine-loaded archive
|
||||||
|
|||||||
@@ -78,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;
|
extern uintptr_t kheap_start;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
+28
-51
@@ -225,69 +225,28 @@ void process_jump_to_user(uintptr_t stack_top, uintptr_t user_code)
|
|||||||
extern struct tss tss;
|
extern struct tss tss;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* process_create_user - Create a new user process
|
* process_create_user_raw - Create a new user process from raw binary
|
||||||
* @file: pointer to Limine file structure
|
* @file: pointer to beginning of binary
|
||||||
|
* @size: size of the binary
|
||||||
* @name: name for the new process
|
* @name: name for the new process
|
||||||
*
|
*
|
||||||
* This function takes a loaded Limine executable
|
* This function takes an executable loaded in memory
|
||||||
* module, and maps its code, a user stack, sets the
|
* and maps its code, a user stack, sets the TSS RSP0
|
||||||
* TSS RSP0 for interrupts, and finally jumps to the
|
* for interrupts, and finally jumps to the user code.
|
||||||
* user code.
|
|
||||||
*/
|
*/
|
||||||
void process_create_user(struct limine_file* file, char* name)
|
|
||||||
{
|
|
||||||
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");
|
|
||||||
|
|
||||||
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;
|
|
||||||
proc->context = ctx;
|
|
||||||
proc->context->iret_ss = USER_DATA_SEGMENT | 3;
|
|
||||||
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;
|
|
||||||
|
|
||||||
uint64_t* user_pml4 = vmm_create_address_space();
|
|
||||||
if (!user_pml4) panic(NULL, "failed to create user address space");
|
|
||||||
proc->root_page_table = user_pml4;
|
|
||||||
|
|
||||||
uintptr_t stack_top = vmm_alloc_user_stack(user_pml4);
|
|
||||||
uint64_t code = vmm_alloc_user_code(user_pml4, exec_addr, exec_size);
|
|
||||||
|
|
||||||
proc->context->iret_rsp = stack_top;
|
|
||||||
proc->context->iret_rip = code;
|
|
||||||
proc->kernel_stack = kalloc_stack();
|
|
||||||
if (!proc->kernel_stack) panic(NULL, "failed to allocate kernel stack");
|
|
||||||
|
|
||||||
// Copy code into user pages; for that we need to temporarily switch to the user pml4
|
|
||||||
load_cr3(VIRT_TO_PHYS((uint64_t)user_pml4));
|
|
||||||
memcpy((uint64_t*)code, exec_addr, exec_size);
|
|
||||||
load_cr3(VIRT_TO_PHYS((uint64_t)kernel_pml4));
|
|
||||||
|
|
||||||
process_add(&processes_list, proc);
|
|
||||||
DEBUG("user process '%s' (pid=%u) enqueued for scheduling", name, proc->pid);
|
|
||||||
SET_INTERRUPTS;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Same as above but for a raw data pointer (pointing to raw binary, no ELF)
|
|
||||||
void process_create_user_raw(char* file, int size, char* name)
|
void process_create_user_raw(char* file, int size, char* name)
|
||||||
{
|
{
|
||||||
|
// Need to refactor this mess
|
||||||
CLEAR_INTERRUPTS;
|
CLEAR_INTERRUPTS;
|
||||||
struct process* proc = (struct process*)kmalloc(sizeof(struct process));
|
struct process* proc = (struct process*)kmalloc(sizeof(struct process));
|
||||||
struct cpu_status* ctx = (struct cpu_status*)kmalloc(sizeof(struct cpu_status));
|
struct cpu_status* ctx = (struct cpu_status*)kmalloc(sizeof(struct cpu_status));
|
||||||
|
|
||||||
if (!proc || !ctx) panic(NULL, "out of memory while creating user process");
|
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);
|
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->pid = next_free_pid++;
|
||||||
proc->status = READY;
|
proc->status = READY;
|
||||||
proc->next = 0;
|
proc->next = 0;
|
||||||
@@ -296,6 +255,24 @@ void process_create_user_raw(char* file, int size, char* name)
|
|||||||
proc->context->iret_cs = USER_CODE_SEGMENT | 3;
|
proc->context->iret_cs = USER_CODE_SEGMENT | 3;
|
||||||
proc->context->iret_flags = 0x202; // Interrupt Flag set
|
proc->context->iret_flags = 0x202; // Interrupt Flag set
|
||||||
|
|
||||||
|
/* 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;
|
void* exec_addr = (void*)file;
|
||||||
uint64_t exec_size = size;
|
uint64_t exec_size = size;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user