Compare commits

..

2 Commits

10 changed files with 63 additions and 48 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"random": "cpp"
}
}

View File

@ -11,8 +11,8 @@
uint16_t identify_result[256]; uint16_t identify_result[256];
uint32_t total_28_lbas = 0; uint32_t total_28_lbas = 0;
uint8_t* rootDirEntries = 0x1000000; uint8_t* rootDirEntries = (uint8_t*)0x1000000;
uint16_t* FAT1 = 0x1002000; uint16_t* FAT1 = (uint16_t*)0x1002000;
uint16_t countReserved; uint16_t countReserved;
uint8_t countFATs; uint8_t countFATs;
@ -60,7 +60,7 @@ void init_atapio() {
// We've initialised now, let's load the FAT and RootDirEntries. // We've initialised now, let's load the FAT and RootDirEntries.
read_sectors(sectorsPerFAT * countFATs + countReserved, countRDEs / 16, rootDirEntries); read_sectors(sectorsPerFAT * countFATs + countReserved, countRDEs / 16, rootDirEntries);
read_sectors(countReserved, sectorsPerFAT, FAT1); read_sectors(countReserved, sectorsPerFAT, (uint8_t*)FAT1);
} }
void read_sector(uint32_t address, uint8_t* buffer) { void read_sector(uint32_t address, uint8_t* buffer) {
@ -95,7 +95,7 @@ uint16_t file_exists(char* filename) {
for (int i=0; i<countRDEs; i++) { for (int i=0; i<countRDEs; i++) {
bool found = strcmp(rootDirEntries+(i*32), filename, 11); bool found = strcmp(rootDirEntries+(i*32), filename, 11);
if (found) { if (found) {
uint16_t* correctEntry = (rootDirEntries+(i*32)); uint16_t* correctEntry = (uint16_t*)(rootDirEntries+(i*32));
return correctEntry[13]; return correctEntry[13];
} }
} }

View File

@ -10,7 +10,7 @@ typedef struct {
uint32_t acpi3_extended; uint32_t acpi3_extended;
}__attribute__((packed)) e820entry; }__attribute__((packed)) e820entry;
uint8_t* bitmap = 0x100000; uint8_t* bitmap = (uint8_t*)0x100000;
void set_bit(uint32_t offset, uint8_t* buffer) { void set_bit(uint32_t offset, uint8_t* buffer) {
uint32_t index = offset / 8; uint32_t index = offset / 8;
@ -61,7 +61,7 @@ void main() {
init_atapio(); init_atapio();
// e820 memory map exists at 0x20000 // e820 memory map exists at 0x20000
e820entry* e820_entries = 0x20000; e820entry* e820_entries = (e820entry*)0x20000;
// Zero out the bitmap. // Zero out the bitmap.
memset(bitmap, 0x20000, 0); memset(bitmap, 0x20000, 0);
@ -93,11 +93,11 @@ void main() {
} }
} }
mark_unavailble(bitmap, 0x20000, bitmap); mark_unavailble((uint32_t)bitmap, 0x20000, bitmap);
mark_unavailble(0, 0xFFFFF, bitmap); mark_unavailble(0, 0xFFFFF, bitmap);
// Page Directory // Page Directory
PDE* kernel_page_directory = bitmap + 0x20000; PDE* kernel_page_directory = (PDE*)(bitmap + 0x20000);
// Clear the PD // Clear the PD
memset((uint8_t*)kernel_page_directory, 4096, 0); memset((uint8_t*)kernel_page_directory, 4096, 0);
@ -110,7 +110,7 @@ void main() {
((uint32_t*)0x521000)[i] = 0x121000 + 0x1000*i; ((uint32_t*)0x521000)[i] = 0x121000 + 0x1000*i;
} }
PTE** kernel_page_tables = 0x521000; PTE** kernel_page_tables = (PTE**)0x521000;
for (int i = 0; i < 1023; i++) { for (int i = 0; i < 1023; i++) {
kernel_page_directory[i] = (PDE){ kernel_page_directory[i] = (PDE){
@ -130,15 +130,15 @@ void main() {
} }
// Mark unavailable bitmap to 0x522000 // Mark unavailable bitmap to 0x522000
mark_unavailble(bitmap, 0x4000000, bitmap); mark_unavailble((uint32_t)bitmap, 0x4000000, bitmap);
// Now we want to map some stuff. // Now we want to map some stuff.
// But first, we should load the kernel somewhere // But first, we should load the kernel somewhere
uint8_t* kernel_location = 0x542000; // Just load it at 0x524000 for now uint8_t* kernel_location = (uint8_t*)0x542000; // Just load it at 0x524000 for now
mark_unavailble(kernel_location, 0x20000, bitmap); // Just treat the kernel as not growing beyond 128k for now. mark_unavailble((uint32_t)kernel_location, 0x20000, bitmap); // Just treat the kernel as not growing beyond 128k for now.
map_many_4k_phys_to_virt(kernel_location, 0xc0000000, kernel_page_directory, kernel_page_tables, 0x20); // Map 32 pages from 0x522000 to 0xc0000000; map_many_4k_phys_to_virt((uint32_t)kernel_location, 0xc0000000, kernel_page_directory, kernel_page_tables, 0x20); // Map 32 pages from 0x522000 to 0xc0000000;
map_4k_phys_to_virt((uint32_t)kernel_page_directory, 0xc0100000, kernel_page_directory, kernel_page_tables); // Map the page directory to 0xc0100000 map_4k_phys_to_virt((uint32_t)kernel_page_directory, 0xc0100000, kernel_page_directory, kernel_page_tables); // Map the page directory to 0xc0100000
map_many_4k_phys_to_virt(0x121000, 0xc0101000, kernel_page_directory, kernel_page_tables, 1024); // Map 1024 pages from 0x121000 to 0xc0101000 map_many_4k_phys_to_virt(0x121000, 0xc0101000, kernel_page_directory, kernel_page_tables, 1024); // Map 1024 pages from 0x121000 to 0xc0101000
map_4k_phys_to_virt(0xb8000, 0xc0501000, kernel_page_directory, kernel_page_tables); // Map 0xb8000 (video) to 0xc0501000 map_4k_phys_to_virt(0xb8000, 0xc0501000, kernel_page_directory, kernel_page_tables); // Map 0xb8000 (video) to 0xc0501000
@ -148,8 +148,8 @@ void main() {
mark_unavailble(0xa0000, 0x20000, bitmap); mark_unavailble(0xa0000, 0x20000, bitmap);
uint8_t* vm_bitmap = 0x522000; uint8_t* vm_bitmap = (uint8_t*)0x522000;
mark_unavailble(vm_bitmap, 0x20000, bitmap); mark_unavailble((uint32_t)vm_bitmap, 0x20000, bitmap);
memset(vm_bitmap, 0x20000, 0xff); memset(vm_bitmap, 0x20000, 0xff);
mark_unavailble(0xc07a0000, 0x20000, vm_bitmap); mark_unavailble(0xc07a0000, 0x20000, vm_bitmap);
@ -189,8 +189,8 @@ void main() {
asm volatile ("mov %0, %%esp":: "r"(0xc1000000 + 16*0x1000)); asm volatile ("mov %0, %%esp":: "r"(0xc1000000 + 16*0x1000));
KernelInformationStruct kstruct= (KernelInformationStruct){ KernelInformationStruct kstruct= (KernelInformationStruct){
.pde = 0xc0100000, .pde = (PDE*)0xc0100000,
.page_directory_phys_addr = kernel_page_directory, .page_directory_phys_addr = (uint32_t)kernel_page_directory,
.page_directory_phys_offset = 0xc0100000 - (uint32_t)kernel_page_directory, .page_directory_phys_offset = 0xc0100000 - (uint32_t)kernel_page_directory,
.page_bitmap_phys = 0xc0600000, .page_bitmap_phys = 0xc0600000,
.page_bitmap_virt = 0xc0620000, .page_bitmap_virt = 0xc0620000,

View File

@ -34,4 +34,5 @@ typedef struct {
void map_4k_phys_to_virt(uint32_t physical, uint32_t virtual, PDE* page_directory, PTE** page_tables); void map_4k_phys_to_virt(uint32_t physical, uint32_t virtual, PDE* page_directory, PTE** page_tables);
void map_many_4k_phys_to_virt(uint32_t physical, uint32_t virtual, PDE* page_directory, PTE** page_tables, uint32_t count); void map_many_4k_phys_to_virt(uint32_t physical, uint32_t virtual, PDE* page_directory, PTE** page_tables, uint32_t count);
void map_many_4k_phys_to_virt_pl3(uint32_t physical, uint32_t virtual, PDE* page_directory, PTE** page_tables, uint32_t count);
#endif #endif

View File

@ -201,11 +201,10 @@ void memcpy(void* dst, void* src, uint32_t count) {
} }
uint32_t exec(uint32_t fh) { uint32_t exec(uint32_t fh) {
char* zero = 0; uint8_t* zero = 0;
return execve(fh, &zero, environ); return execve(fh, &zero, (uint8_t**)environ);
} }
uint32_t execv(uint32_t fh, char** argv) { uint32_t execv(uint32_t fh, char** argv) {
char* zero = 0; return execve(fh, (uint8_t**)argv, (uint8_t**)environ);
return execve(fh, argv, environ);
} }

View File

@ -38,6 +38,7 @@ void handle_fault(frame_struct* frame) {
case 13: // GPF case 13: // GPF
Global::kernel->terminal->printf("General Protection Fault!"); Global::kernel->terminal->printf("General Protection Fault!");
if (frame->eflags & 0x00020000) { if (frame->eflags & 0x00020000) {
Global::kernel->terminal->printf("\x1b[42;37;1mv86 GPF! All is good!\n");
v86_monitor((v8086_frame_struct*)frame); v86_monitor((v8086_frame_struct*)frame);
return; return;
} }
@ -56,7 +57,7 @@ void handle_fault(frame_struct* frame) {
while (1) asm("hlt"); while (1) asm("hlt");
} else { } else {
// Print an error message. // Print an error message.
//Global::kernel->terminal->printf("PID %d Terminated due to fault!\n", Global::currentProc->PID); Global::kernel->terminal->printf("PID %d Terminated due to fault!\n", Global::currentProc->PID);
asm volatile ("mov %0, %%esp" ::"m"(Global::kernel->globalISRStack)); asm volatile ("mov %0, %%esp" ::"m"(Global::kernel->globalISRStack));
Global::kernel->PD->select(); Global::kernel->PD->select();
@ -68,7 +69,7 @@ void handle_fault(frame_struct* frame) {
} }
} }
void ignore_interrupt() {} void ignore_interrupt(frame_struct* _) {}
void context_switch() { void context_switch() {
frame_struct* frame = &Global::currentThread->frame; frame_struct* frame = &Global::currentThread->frame;
@ -131,7 +132,7 @@ void context_switch() {
// Select the next processes page directory // Select the next processes page directory
Global::currentThread->frame.new_cr3 = Global::currentThread->parent->PD->phys_addr; Global::currentThread->frame.new_cr3 = Global::currentThread->parent->PD->phys_addr;
Global::tss->esp0 = Global::currentThread->kernelStackPtrDefault; Global::tss->esp0 = (uint32_t)Global::currentThread->kernelStackPtrDefault;
// Set the current proc to valid // Set the current proc to valid
Global::currentProcValid = true; Global::currentProcValid = true;
@ -141,7 +142,7 @@ namespace Timer {
// counter, default count, function, argument, oneshot // counter, default count, function, argument, oneshot
using TimedEvent = xnoe::tuple<uint32_t, uint32_t, void(*)(void*), void*, bool>; using TimedEvent = xnoe::tuple<uint32_t, uint32_t, void(*)(void*), void*, bool>;
xnoe::linkedlist<TimedEvent> timed_events; xnoe::linkedlist<TimedEvent> timed_events;
void tick() { void tick(frame_struct* _) {
xnoe::linkedlistelem<TimedEvent>* current = timed_events.start; xnoe::linkedlistelem<TimedEvent>* current = timed_events.start;
while (current) { while (current) {
TimedEvent t = current->elem; TimedEvent t = current->elem;
@ -173,7 +174,7 @@ void awaken(Thread* t) {
t->state = Running; t->state = Running;
} }
void syscall() { void syscall(frame_struct* _) {
// Syscall ABI: // Syscall ABI:
// 0: getDentsSize :: char* path -> uint32_t size // 0: getDentsSize :: char* path -> uint32_t size
// 1: getDents :: char* path -> uint8_t* buffer -> void // 1: getDents :: char* path -> uint8_t* buffer -> void
@ -217,19 +218,19 @@ void syscall() {
switch (frame->eax) { switch (frame->eax) {
case 0: // getDentsSize case 0: // getDentsSize
frame->eax = Global::kernel->rootfs->getDentsSize(createPathFromString(frame->ebx)); frame->eax = Global::kernel->rootfs->getDentsSize(createPathFromString((char*)frame->ebx));
break; break;
case 1: // getDents case 1: // getDents
Global::kernel->rootfs->getDents(createPathFromString(frame->ebx), frame->ecx); Global::kernel->rootfs->getDents(createPathFromString((char*)frame->ebx), (FSDirectoryListing*)frame->ecx);
break; break;
case 2: // exists case 2: // exists
frame->eax = Global::kernel->rootfs->exists(createPathFromString(frame->ebx)); frame->eax = Global::kernel->rootfs->exists(createPathFromString((char*)frame->ebx));
break; break;
case 3: // type case 3: // type
frame->eax = Global::kernel->rootfs->type(createPathFromString(frame->ebx)); frame->eax = Global::kernel->rootfs->type(createPathFromString((char*)frame->ebx));
break; break;
case 4: // malloc case 4: // malloc
frame->eax = currentProc->allocate(frame->ebx); frame->eax = (uint32_t)currentProc->allocate(frame->ebx);
break; break;
case 5: // free case 5: // free
currentProc->deallocate(frame->ebx); currentProc->deallocate(frame->ebx);
@ -277,7 +278,7 @@ void syscall() {
} }
ReadWriter* rw = fh.get(); ReadWriter* rw = fh.get();
frame->eax = rw->read(frame->ebx, frame->edx); frame->eax = rw->read(frame->ebx, (uint8_t*)frame->edx);
break; break;
} }
@ -289,7 +290,7 @@ void syscall() {
} }
ReadWriter* rw = fh.get(); ReadWriter* rw = fh.get();
frame->eax = rw->write(frame->ebx, frame->edx); frame->eax = rw->write(frame->ebx, (uint8_t*)frame->edx);
break; break;
} }
@ -330,7 +331,7 @@ void syscall() {
} }
case 15: { // fopen case 15: { // fopen
ReadWriter* file = Global::kernel->rootfs->open(createPathFromString(frame->ebx)); ReadWriter* file = Global::kernel->rootfs->open(createPathFromString((char*)frame->ebx));
if (file) if (file)
frame->eax = Global::currentProc->mapFH(file); frame->eax = Global::currentProc->mapFH(file);
break; break;
@ -358,7 +359,7 @@ void syscall() {
case 18: { // sleep case 18: { // sleep
Global::currentThread->state = Suspended; Global::currentThread->state = Suspended;
Timer::register_event(frame->ebx, &awaken, (void*)Global::currentThread, true); Timer::register_event(frame->ebx, (void(*)(void*))&awaken, (void*)Global::currentThread, true);
context_switch(); context_switch();
break; break;
} }
@ -400,7 +401,7 @@ void syscall() {
case 24: { // spawnThread case 24: { // spawnThread
Thread* thread = new Thread(Global::currentProc); Thread* thread = new Thread(Global::currentProc);
thread->initKernelStack(frame->ebx, thread->stack); thread->initKernelStack((void*)frame->ebx, thread->stack);
Global::kernel->registerThread(thread); Global::kernel->registerThread(thread);
break; break;
} }
@ -449,7 +450,7 @@ void v86_monitor(v8086_frame_struct* frame) {
} }
case 0xcf: { case 0xcf: {
// Handle iret // Handle iret
frame->eip = *(sp++); frame->eip = *(sp++) + 2;
frame->cs = *(sp++); frame->cs = *(sp++);
frame->eflags &= 0xffff0000; frame->eflags &= 0xffff0000;
frame->eflags |= *(sp++); frame->eflags |= *(sp++);
@ -467,7 +468,7 @@ void v86_monitor(v8086_frame_struct* frame) {
break; break;
} }
} }
frame->esp = sp; frame->esp = (uint16_t)sp;
} }
void init_idt() { void init_idt() {
@ -520,7 +521,7 @@ void init_idt() {
outb(0x40, _counter[0]); outb(0x40, _counter[0]);
outb(0x40, _counter[1]); outb(0x40, _counter[1]);
Timer::register_event(30, &context_switch, 0); Timer::register_event(30, (void(*)(void*))&context_switch, 0);
} }
void enable_idt() { void enable_idt() {

View File

@ -51,6 +51,15 @@ int main(KernelInformationStruct kstruct) {
term->activate(); term->activate();
term->clear_screen(); term->clear_screen();
/*kernel.v86(
0x4F00,
0,
0,
0,
0,
0x10
);*/
term->printf("Hello, World!\n\nWe are running XnoeOS Code in C++ now, Protected Mode has been achieved (as well as Virtual Memory / Paging!!!) and everything is working super nicely!\n\nHow wonderful!\n\nNow I just need to hope my print function works properly too~~\n"); term->printf("Hello, World!\n\nWe are running XnoeOS Code in C++ now, Protected Mode has been achieved (as well as Virtual Memory / Paging!!!) and everything is working super nicely!\n\nHow wonderful!\n\nNow I just need to hope my print function works properly too~~\n");
term->printf("KERNEL OK!\n"); term->printf("KERNEL OK!\n");

View File

@ -6,11 +6,11 @@ int main() {
bindToKeyboard(); bindToKeyboard();
char* fn = "/xosh.bin"; char* fn = "/xosh.bin";
char* argv[] = { uint8_t* argv[] = {
fn, fn,
0 0
}; };
char* envp[] = { uint8_t* envp[] = {
"PATH=/", "PATH=/",
0 0
}; };

View File

@ -19,14 +19,14 @@ char* filetype(FSType t) {
int main(int argc, char** argv) { int main(int argc, char** argv) {
if (argc != 2) { if (argc != 2) {
printf("Usage: %s <directory>", argv[0]); printf("Usage: %s <directory>", argv[0]);
return; return 1;
} }
char* directory = argv[1]; char* directory = argv[1];
uint32_t dentsSize = getDentsSize(directory); uint32_t dentsSize = getDentsSize(directory);
FSDirectoryListing* dents = (FSDirectoryListing*)malloc(dentsSize); FSDirectoryListing* dents = (FSDirectoryListing*)malloc(dentsSize);
getDents(directory, dents); getDents(directory, (uint8_t*)dents);
for (int i=0; i<dents->count; i++) { for (int i=0; i<dents->count; i++) {
FSDirectoryEntry e = dents->entries[i]; FSDirectoryEntry e = dents->entries[i];

View File

@ -53,7 +53,7 @@ int main(int argc, char** argv, char** envp) {
// Parse the input // Parse the input
char* result[32]; char* result[32];
uint32_t ridx=0; uint32_t ridx=0;
memset(result, 0, 128); memset(result, 0, 32 * sizeof(char));
char c; char c;
uint32_t idx=0; uint32_t idx=0;
char* lp = input; char* lp = input;
@ -98,7 +98,7 @@ int main(int argc, char** argv, char** envp) {
uint32_t e = execv(program, argv); uint32_t e = execv(program, argv);
printf("\nExit code: %d\n\n", e); printf("\nExit code: %d\n\n", e);
} else if (strcmp(result[0], "set")) { } else if (strcmp(result[0], "set")) {
setenv(result[1], result[2]); if (result[1] && result[2]) setenv(result[1], result[2]);
} else { } else {
printf("No such executable file: `%s`\n\n", programName); printf("No such executable file: `%s`\n\n", programName);
} }