starting to work on pty drivers, fork(), and scheduler redesign
This commit is contained in:
Binary file not shown.
36
programs/test/compile_commands.json
Normal file
36
programs/test/compile_commands.json
Normal file
@@ -0,0 +1,36 @@
|
||||
[
|
||||
{
|
||||
"arguments": [
|
||||
"/home/slinky/opt/cross/bin/i686-elf-gcc",
|
||||
"-ffreestanding",
|
||||
"-O2",
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-I../../rocklibc/include",
|
||||
"-c",
|
||||
"-o",
|
||||
"crt0.o",
|
||||
"crt0.S"
|
||||
],
|
||||
"directory": "/home/slinky/source/RockOS/programs/test",
|
||||
"file": "/home/slinky/source/RockOS/programs/test/crt0.S",
|
||||
"output": "/home/slinky/source/RockOS/programs/test/crt0.o"
|
||||
},
|
||||
{
|
||||
"arguments": [
|
||||
"/home/slinky/opt/cross/bin/i686-elf-gcc",
|
||||
"-ffreestanding",
|
||||
"-O2",
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-I../../rocklibc/include",
|
||||
"-c",
|
||||
"-o",
|
||||
"main.o",
|
||||
"main.c"
|
||||
],
|
||||
"directory": "/home/slinky/source/RockOS/programs/test",
|
||||
"file": "/home/slinky/source/RockOS/programs/test/main.c",
|
||||
"output": "/home/slinky/source/RockOS/programs/test/main.o"
|
||||
}
|
||||
]
|
||||
21
programs/test/crt0.S
Normal file
21
programs/test/crt0.S
Normal file
@@ -0,0 +1,21 @@
|
||||
.global _start
|
||||
.extern exit
|
||||
.section .text
|
||||
_start:
|
||||
xor %ebp, %ebp
|
||||
|
||||
mov (%esp), %eax
|
||||
lea 4(%esp), %ebx
|
||||
lea 8(%esp,%eax,4), %ecx
|
||||
|
||||
and $-16, %esp
|
||||
|
||||
push %ecx
|
||||
push %ebx
|
||||
push %eax
|
||||
|
||||
call main
|
||||
|
||||
push %eax
|
||||
call exit
|
||||
|
||||
31
programs/test/linker.ld
Normal file
31
programs/test/linker.ld
Normal file
@@ -0,0 +1,31 @@
|
||||
ENTRY(_start)
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
. = 0x40000000;
|
||||
|
||||
.text ALIGN(4K) :
|
||||
{
|
||||
/* Force the crt0.o entry code to be placed FIRST in memory */
|
||||
KEEP(*crt0.o(.text))
|
||||
*(.text .text.*)
|
||||
}
|
||||
|
||||
.rodata ALIGN(4K) :
|
||||
{
|
||||
*(.rodata .rodata.*)
|
||||
}
|
||||
|
||||
.data ALIGN(4K) :
|
||||
{
|
||||
*(.data .data.*)
|
||||
}
|
||||
|
||||
.bss ALIGN(4K) :
|
||||
{
|
||||
_bss_start = .;
|
||||
*(.bss .bss.*)
|
||||
*(COMMON)
|
||||
_bss_end = .;
|
||||
}
|
||||
}
|
||||
12
programs/test/main.c
Normal file
12
programs/test/main.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
|
||||
if (!fork()) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
23
programs/test/makefile
Normal file
23
programs/test/makefile
Normal file
@@ -0,0 +1,23 @@
|
||||
CC = i686-elf-gcc
|
||||
LD = i686-elf-ld
|
||||
|
||||
CFLAGS = -ffreestanding -O2 -Wall -Wextra -I../../rocklibc/include
|
||||
LDFLAGS = -m elf_i386 -nostdlib
|
||||
|
||||
TARGET = test.elf
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
crt0.o: crt0.S
|
||||
$(CC) $(CFLAGS) -c crt0.S -o crt0.o
|
||||
|
||||
main.o: main.c
|
||||
$(CC) $(CFLAGS) -c main.c -o main.o
|
||||
|
||||
$(TARGET): crt0.o main.o ../lib/rlibc.a
|
||||
$(LD) $(LDFLAGS) -T linker.ld crt0.o main.o ../lib/rlibc.a -o $(TARGET)
|
||||
|
||||
clean:
|
||||
rm -f *.o $(TARGET)
|
||||
|
||||
.PHONY: all clean test
|
||||
36
programs/vga_text_term/compile_commands.json
Normal file
36
programs/vga_text_term/compile_commands.json
Normal file
@@ -0,0 +1,36 @@
|
||||
[
|
||||
{
|
||||
"arguments": [
|
||||
"/home/slinky/opt/cross/bin/i686-elf-gcc",
|
||||
"-ffreestanding",
|
||||
"-O2",
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-I../../rocklibc/include",
|
||||
"-c",
|
||||
"-o",
|
||||
"crt0.o",
|
||||
"crt0.S"
|
||||
],
|
||||
"directory": "/home/slinky/source/RockOS/programs/vga_text_term",
|
||||
"file": "/home/slinky/source/RockOS/programs/vga_text_term/crt0.S",
|
||||
"output": "/home/slinky/source/RockOS/programs/vga_text_term/crt0.o"
|
||||
},
|
||||
{
|
||||
"arguments": [
|
||||
"/home/slinky/opt/cross/bin/i686-elf-gcc",
|
||||
"-ffreestanding",
|
||||
"-O2",
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-I../../rocklibc/include",
|
||||
"-c",
|
||||
"-o",
|
||||
"main.o",
|
||||
"main.c"
|
||||
],
|
||||
"directory": "/home/slinky/source/RockOS/programs/vga_text_term",
|
||||
"file": "/home/slinky/source/RockOS/programs/vga_text_term/main.c",
|
||||
"output": "/home/slinky/source/RockOS/programs/vga_text_term/main.o"
|
||||
}
|
||||
]
|
||||
21
programs/vga_text_term/crt0.S
Normal file
21
programs/vga_text_term/crt0.S
Normal file
@@ -0,0 +1,21 @@
|
||||
.global _start
|
||||
.extern exit
|
||||
.section .text
|
||||
_start:
|
||||
xor %ebp, %ebp
|
||||
|
||||
mov (%esp), %eax
|
||||
lea 4(%esp), %ebx
|
||||
lea 8(%esp,%eax,4), %ecx
|
||||
|
||||
and $-16, %esp
|
||||
|
||||
push %ecx
|
||||
push %ebx
|
||||
push %eax
|
||||
|
||||
call main
|
||||
|
||||
push %eax
|
||||
call exit
|
||||
|
||||
31
programs/vga_text_term/linker.ld
Normal file
31
programs/vga_text_term/linker.ld
Normal file
@@ -0,0 +1,31 @@
|
||||
ENTRY(_start)
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
. = 0x40000000;
|
||||
|
||||
.text ALIGN(4K) :
|
||||
{
|
||||
/* Force the crt0.o entry code to be placed FIRST in memory */
|
||||
KEEP(*crt0.o(.text))
|
||||
*(.text .text.*)
|
||||
}
|
||||
|
||||
.rodata ALIGN(4K) :
|
||||
{
|
||||
*(.rodata .rodata.*)
|
||||
}
|
||||
|
||||
.data ALIGN(4K) :
|
||||
{
|
||||
*(.data .data.*)
|
||||
}
|
||||
|
||||
.bss ALIGN(4K) :
|
||||
{
|
||||
_bss_start = .;
|
||||
*(.bss .bss.*)
|
||||
*(COMMON)
|
||||
_bss_end = .;
|
||||
}
|
||||
}
|
||||
5
programs/vga_text_term/main.c
Normal file
5
programs/vga_text_term/main.c
Normal file
@@ -0,0 +1,5 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
return 0;
|
||||
}
|
||||
23
programs/vga_text_term/makefile
Normal file
23
programs/vga_text_term/makefile
Normal file
@@ -0,0 +1,23 @@
|
||||
CC = i686-elf-gcc
|
||||
LD = i686-elf-ld
|
||||
|
||||
CFLAGS = -ffreestanding -O2 -Wall -Wextra -I../../rocklibc/include
|
||||
LDFLAGS = -m elf_i386 -nostdlib
|
||||
|
||||
TARGET = vga_text_term.elf
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
crt0.o: crt0.S
|
||||
$(CC) $(CFLAGS) -c crt0.S -o crt0.o
|
||||
|
||||
main.o: main.c
|
||||
$(CC) $(CFLAGS) -c main.c -o main.o
|
||||
|
||||
$(TARGET): crt0.o main.o ../lib/rlibc.a
|
||||
$(LD) $(LDFLAGS) -T linker.ld crt0.o main.o ../lib/rlibc.a -o $(TARGET)
|
||||
|
||||
clean:
|
||||
rm -f *.o $(TARGET)
|
||||
|
||||
.PHONY: all clean test
|
||||
104
programs/vga_text_term/syscall.c
Normal file
104
programs/vga_text_term/syscall.c
Normal file
@@ -0,0 +1,104 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "memory/mm.h"
|
||||
#include "process.h"
|
||||
#include "scheduler.h"
|
||||
#include "kbd.h"
|
||||
|
||||
#include <drivers/vga/vga.h>
|
||||
|
||||
#include <lib/print.h>
|
||||
|
||||
|
||||
static int32_t sys_exit(int status) {
|
||||
process_t* task = current_task();
|
||||
task->state = STATE_DEAD;
|
||||
kprintf("Process %d exited with status %d\n", task->pid, status);
|
||||
exit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t sys_write(int fd, const void* buf, size_t count) {
|
||||
if (fd == 1 || fd == 2) { // stdout or stderr
|
||||
const char* cbuf = (const char*)buf;
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
vga_putchar(cbuf[i]);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int32_t sys_read(int fd, void* buf, size_t count) {
|
||||
if (fd == 0) {
|
||||
char* cbuf = (char*)buf;
|
||||
size_t bytes_read = 0;
|
||||
|
||||
while (bytes_read < count) {
|
||||
kbd_wait();
|
||||
|
||||
if (kbd_ready()) {
|
||||
char c = kbd_read();
|
||||
cbuf[bytes_read++] = c;
|
||||
if (c == '\n') break;
|
||||
}
|
||||
}
|
||||
return bytes_read;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int32_t sys_open(const char* filename, int flags) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int32_t sys_close(int fd) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int32_t sys_brk(uint32_t new_break) {
|
||||
process_t* current = current_task();
|
||||
|
||||
if (new_break == 0) {
|
||||
return current->heap_end;
|
||||
}
|
||||
|
||||
if (new_break < current->heap_end) {
|
||||
current->heap_end = new_break;
|
||||
return current->heap_end;
|
||||
}
|
||||
|
||||
uint32_t page_start = (current->heap_end + 4095) & ~4095;
|
||||
uint32_t page_end = (new_break + 4095) & ~4095;
|
||||
|
||||
for (uint32_t addr = page_start; addr < page_end; addr += 4096) {
|
||||
// 1. Allocate a physical frame using your PMM (Physical Memory Manager)
|
||||
void* phys = p_alloc_frame();
|
||||
|
||||
// 2. Map it into the current page directory using your VMM (Virtual Memory Manager)
|
||||
map_page(addr, (uint32_t)phys, PAGE_PRESENT | PAGE_WRITABLE | PAGE_USER);
|
||||
}
|
||||
|
||||
current->heap_end = new_break;
|
||||
return current->heap_end;
|
||||
}
|
||||
|
||||
typedef struct registers {
|
||||
uint32_t gs, fs, es, ds; // Pushed manually
|
||||
uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax; // Pushed by pusha
|
||||
uint32_t int_no, err_code; // Pushed manually
|
||||
uint32_t eip, cs, eflags, useresp, ss; // Pushed automatically by CPU
|
||||
} registers_t;
|
||||
int32_t syscall(registers_t* regs) {
|
||||
switch(regs->eax) {
|
||||
case 1: return sys_exit(regs->ebx);
|
||||
case 3: return sys_read(regs->ebx, (void*)regs->ecx, regs->edx);
|
||||
case 4: return sys_write(regs->ebx, (const void*)regs->ecx, regs->edx);
|
||||
case 5: return sys_open((const char*)regs->ebx, regs->ecx);
|
||||
case 6: return sys_close(regs->ebx);
|
||||
case 12: return sys_brk(regs->ebx);
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user