summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorMiguel <m.i@gmx.at>2018-08-23 03:20:56 +0200
committerMiguel <m.i@gmx.at>2018-08-23 03:20:56 +0200
commit4404fa9b3d98646f942e32146722a9d0a68edc13 (patch)
tree79e494ec81a462db0217fc763a7ddae1827c02bd /kernel
parent98bf7b67543b36b6fe49f2b68c115ebeaf630603 (diff)
never ending struggle with forking
Diffstat (limited to 'kernel')
-rw-r--r--kernel/gdt.c23
-rw-r--r--kernel/kernel.c70
-rw-r--r--kernel/kernel.h7
-rw-r--r--kernel/log.c2
-rw-r--r--kernel/mem.c6
-rw-r--r--kernel/multiboot.h11
-rw-r--r--kernel/scheduler.c20
-rw-r--r--kernel/smp.c9
-rw-r--r--kernel/syscalls.c9
-rw-r--r--kernel/usermode.c6
-rw-r--r--kernel/vmem.c71
11 files changed, 119 insertions, 115 deletions
diff --git a/kernel/gdt.c b/kernel/gdt.c
index dc760f2..4df089f 100644
--- a/kernel/gdt.c
+++ b/kernel/gdt.c
@@ -7,13 +7,13 @@
#define GDT_SIZE 6
extern sys_tss;
+static uint8_t gdt_struct[GDT_SIZE*8];
typedef struct GDT_struct
{
uint32_t base;
uint32_t limit;
uint32_t type;
-
}GDT;
//alternative
@@ -38,10 +38,6 @@ struct gdt_entry_bits
unsigned int base_high :8;
} __packed; //or __attribute__((packed))
-
-static GDT myGDT[GDT_SIZE];
-static uint8_t gdt_struct[GDT_SIZE*8];
-
/**
* \param target A pointer to the 8-byte GDT entry
* \param source An arbitrary structure describing the GDT entry
@@ -89,6 +85,16 @@ void encodeGdtEntry(uint8_t *target, GDT source)
void gdt_init()
{
+ /*
+ Pr=1 Privl 1 Exec DC RW Ac
+ 0x9A == 1001 1010 == 1 00 1 1 0 1 0
+ 0x92 == 1001 0010 == 1 00 1 0 0 1 0
+ 0xFA == 1111 1010 == 1 11 1 1 0 1 0
+ 0xF2 == 1111 0010 == 1 11 1 0 0 1 0
+ */
+
+ GDT myGDT[GDT_SIZE];
+
//selector 0x0
myGDT[0].base=0;
myGDT[0].limit=0;
@@ -115,15 +121,16 @@ void gdt_init()
myGDT[4].type=0xF2;
//TSS 0x28
- myGDT[5].base=&sys_tss; //tss start?
- myGDT[5].limit=sizeof(tss_struct); //tss end?
+ myGDT[5].base=&sys_tss; //tss start
+ myGDT[5].limit=sizeof(tss_struct); //tss end
myGDT[5].type=0x89;
// transcript to format the processor wants
for(int i=0;i<GDT_SIZE;i++)
encodeGdtEntry(&gdt_struct[8*i],myGDT[i]);
- // updat
+ // update tss entry
install_tss();
+
setup_gdt(&gdt_struct[0],8*GDT_SIZE);
}
diff --git a/kernel/kernel.c b/kernel/kernel.c
index 7784e64..97aef77 100644
--- a/kernel/kernel.c
+++ b/kernel/kernel.c
@@ -1,26 +1,24 @@
+#include <stdint.h>
#include "kernel/kernel.h"
-#include <stdint.h>
+#include "kernel/mem.h"
+#include "kernel/vmem.h"
+#include "kernel/gdt.h"
+#include "kernel/scheduler.h"
+#include "kernel/multiboot.h"
#include "driver/serial.h"
#include "driver/timer.h"
#include "driver/keyboard.h"
#include "driver/mouse.h"
-#include "kernel/gdt.h"
-#include "kernel/scheduler.h"
-
#include "syscalls.h"
#include "types.h"
#include "fifo.h"
-#include "mem.h"
-#include "vmem.h"
#include "mp.h"
#include "interrupts.h"
-#include "multiboot.h"
#include "ringbuffer.h"
-#include "multiboot.h"
#include "terminal/terminal.h"
#include "driver/screen.h"
@@ -28,64 +26,62 @@
void kernel_main(uint32_t eax,uint32_t ebx)
{
serial_init();
+ klog("FOOL-OS ver-%s (%s)",GIT_REVISION,__DATE__);
- klog ("%s - BUILD: git-%s (%s %s)",KERNEL_NAME,GIT_REVISION,__DATE__,__TIME__);
-
- klog("COM 1 - initialized");
-
- uint64_t epoch_time=timer_init();
- klog("PIT - initialized. %u seconds passed since 1970.",epoch_time);
+ klog("Programmable Interval Timer (PIT) init ...");
+ uint64_t unixtime=timer_init();
+ klog("Unix Time = %u seconds)",unixtime);
- keyboard_init(0); //sstdin
- klog("Keyboard Initialized");
+ klog("Keyboard init ...");
+ keyboard_init(0);
+ klog("Mouse init ...");
mouse_init();
- klog("Mouse Initialized");
+ klog("Global Descriptor Table (GDT) init ...");
gdt_init();
- klog("GDT Initialized");
- // MULTIBOOT HEADER
- multiboot_information *info=get_multiboot(eax, ebx);
+ klog("Multiboot Structures init ... ");
+ multiboot_information *info;
+ info=get_multiboot(eax, ebx);
- // Gather Info about other processors. (APs = application processors) // ACPI or MP
- //smp_processors procdata;
- //
- //if(!acpi_find(&procdata))
- // if(!mp_find(&procdata))
- // kpanic("ACPI and MP search failed! I do not want to continue!");
-
- // MEMORY INIT (allows allocating and deaclloating physical memory)
+ klog("Symmetrical Multi Processing (SMP) init ... ");
+ smp_processors procdata;
+ if(!acpi_find(&procdata)&&!mp_find(&procdata))kpanic("No ACPI or MP found!");
+
+ klog("Memory init ... ");
uint32_t kernel_blocks=mem_init(info);
- // Mount Root EXT2 ramimage (needs to be done before other processors started, because of /boot/mp.bin)
+ klog("Ram Filesystem init ... "); // required by mp.bin ????
fs_mount(info);
// Start the other Processors (before paging because apic addr etc..?)
//TODO: !!! Check commented out sleep ()!!!
// https://wiki.osdev.org/Symmetric_Multiprocessing
- // smp_log_procdata(&procdata);
- // smp_start_aps(&procdata,"/boot/mp.bin"); //will be copied over mbr
+ klog("Symmetric Multi Processing (SMP) start ... ");
+ smp_log_procdata(&procdata);
+ //smp_start_aps(&procdata,"/boot/mp.bin"); //will be copied over mbr
- // VIRTUAL MEMORY (paging)
+ klog("Vritual Memory / Paging init ... ");
pdirectory *dir=vmem_init(kernel_blocks,(uint32_t)info->framebuffer_addr);
- // PCI Bus
- //pci_init();
+ klog("Peripheral Component Interconnet (PCI) init ... ");
+ pci_init();
- // INIT VESA: TODO: stop and say if not 32bit colormode!
+ klog("Video Electronics Standards Association (VESA) init ... "); // TODO check if text or fb?
uint32_t addr=kballoc(1);
fs_content("/binfont.bin",addr,0x100); // copy 0x100 bytes to 0x7000
-
vesa_init(info->vbe_control_info,info->vbe_mode_info,addr);
- // STREAMS
+ klog("stdin/stdout init ...");
uint32_t sstdin = syscall_open("stdin",0,0); // stdin 0
uint32_t sstdout = syscall_open("term",0,0); // stdout 1
uint32_t sstderr = syscall_open("stderr",0,0); // stderr 2
+ klog("Interrupt Vector Table (IVT) init ...");
interrupts_init(0x08);
+ klog("Enable Interrupts & Start Scheduling ...");
scheduler_init(dir);
}
diff --git a/kernel/kernel.h b/kernel/kernel.h
index f1ce9d3..1949af5 100644
--- a/kernel/kernel.h
+++ b/kernel/kernel.h
@@ -5,22 +5,21 @@
#ifndef FOOLOS_CONFIG_H
#define FOOLOS_CONFIG_H
-#define KERNEL_NAME "F00l-Os"
#define BIN_INIT "/bin/init"
+//#define FOOLOS_LOG_OFF
#define FIFO_MAX_RINGBUFFERS 20
#define MAX_FIFOS 20
#define MAX_FD 20
#define MAX_TASKS 255
#define MEM_PRINT_MEMORYMAP
-//#define FOOLOS_LOG_OFF // do not log anything
//#define LOG_SYSCALLS
#define KMALLOC_MEM_SIZE 1024*1024*8 // 8MB for in kernel-memory
#define NUMBER_SPINLOCKS 16
-#define kpanic(...) log(__FILE__,0,__VA_ARGS__)
-#define klog(...) log(__FILE__,10,__VA_ARGS__)
+#define kpanic(...) {log(__FILE__,0," \033[41;37m--PANIC--\033[37;40m " __VA_ARGS__ ); while(1);}
+#define klog(...) log(__FILE__,10, __VA_ARGS__)
#endif
diff --git a/kernel/log.c b/kernel/log.c
index 3986592..8ab2bf2 100644
--- a/kernel/log.c
+++ b/kernel/log.c
@@ -39,7 +39,7 @@ void log(char *module_name, int prio, char *format_string, ...)
tfp_vsprintf(buf_info,format_string,va);
va_end(va);
- tfp_sprintf(buf_log,"\033[36;40m%s\033[31;40m %10s:\033[37;40m %s\n",buf_time,module_name,buf_info);
+ tfp_sprintf(buf_log,"\033[36;40m%s\033[31;40m %s:\033[37;40m %s\n",buf_time,module_name,buf_info);
log_string(buf_log);
}
diff --git a/kernel/mem.c b/kernel/mem.c
index c5fe43f..e05ca3e 100644
--- a/kernel/mem.c
+++ b/kernel/mem.c
@@ -119,7 +119,7 @@ void* pmmngr_alloc_block ()
mem_free_blocks--;
uint32_t addr = frame * PMMNGR_BLOCK_SIZE;
- klog("alloc block (%d) 0x%08X)",frame,addr);
+ //klog("alloc block (%d) 0x%08X)",frame,addr);
return (void*)addr;
}
@@ -137,12 +137,12 @@ void pmmngr_free_block (void* p)
}
else
{
- klog("free block (%d) 0x%08X)",frame,addr);
+ //klog("free block (%d) 0x%08X)",frame,addr);
kpanic("trying to free, free physical mem!");
}
- klog("free block (%d) 0x%08X)",frame,addr);
+ //klog("free block (%d) 0x%08X)",frame,addr);
}
diff --git a/kernel/multiboot.h b/kernel/multiboot.h
index 560bae4..49f239f 100644
--- a/kernel/multiboot.h
+++ b/kernel/multiboot.h
@@ -1,12 +1,18 @@
+/**
+ * @file
+ *
+ * This Structures ars defined by the multiboot specification and you will
+ * get them from your bootloader.
+ */
+
//# https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#Boot-information-format
+
#ifndef MULTIBOOT_H
#define MULTIBOOT_H
-
#include <stdbool.h>
#include <stdint.h>
-
typedef struct multiboot_information_struct
{
uint32_t flags;
@@ -58,7 +64,6 @@ typedef struct multiboot_mod_struct
}multiboot_mod;
-
multiboot_information* get_multiboot(uint32_t eax, uint32_t ebx);
#endif
diff --git a/kernel/scheduler.c b/kernel/scheduler.c
index 5cc5508..540e72b 100644
--- a/kernel/scheduler.c
+++ b/kernel/scheduler.c
@@ -35,12 +35,23 @@ volatile int add_task(uint32_t esp, uint32_t vmem)
{
task_list[i].parent=current_task;
task_list[i].vmem=vmem;
- task_list[i].esp=esp;
+ task_list[i].esp = kballoc(4)+4*4096;
task_list[i].esp0 = kballoc(4)+4*4096;
task_list[i].active=true;
task_list[i].waiting=false;
task_list[i].skipwait=false;
task_list[i].brk=task_list[current_task].brk;
+
+ uint32_t *addi2=(uint32_t *)esp;
+ addi2+=14;
+
+ for(int x=0;x<15;x++)
+ {
+ task_list[i].esp-=4;
+ uint32_t *addi1=(uint32_t *)task_list[i].esp;
+ *addi1=*addi2;
+ addi2--;
+ }
return i;
}
}
@@ -168,17 +179,16 @@ volatile void scheduler_init(pdirectory *dir)
task_list[1].waiting=false;
task_list[1].vmem=dir;
task_list[1].esp = kballoc(4)+4*4096;
- task_list[1].esp0 = 0; // not needed by kernel space task
+ task_list[1].esp0 = 0; // not needed by kernel space tasks
- task_pusha(task_list[1].esp); // pusha but to alternative location
- task_pusha(task_list[0].esp); // pusha but to alternative location
+ task_pusha(task_list[1].esp);
+ task_pusha(task_list[0].esp);
// finally enable interrrupts so the scheduler is called (by timer)
x86_sti();
// loop until scheduler kicks in and reschedules us...
while(1);
-
}
volatile int task_get_current_pid()
diff --git a/kernel/smp.c b/kernel/smp.c
index d4bb6a1..4fe0705 100644
--- a/kernel/smp.c
+++ b/kernel/smp.c
@@ -3,9 +3,6 @@
// http://download.intel.com/design/chipsets/datashts/29056601.pdf
// http://www.scs.stanford.edu/05au-cs240c/lab/ia32/IA32-3.pdf
-
-
-
#include <stdint.h>
#include "smp.h"
#include "mem.h"
@@ -28,7 +25,7 @@ void smp_main()
{
x86_cli();
- klog("local apic_addr:0x%08X",local_apic_addr);
+// klog("local apic_addr:0x%08X",local_apic_addr);
while(1); //TODO!!!
@@ -127,7 +124,3 @@ void smp_start_aps(smp_processors *pros,char *path)
*reg=(6<<8)|(1<<14)|0x7; // 110 SIPI
}
}
-
-
-
-
diff --git a/kernel/syscalls.c b/kernel/syscalls.c
index 9f8d136..ba9d68f 100644
--- a/kernel/syscalls.c
+++ b/kernel/syscalls.c
@@ -1,5 +1,3 @@
-
-
#include "lib/string/string.h"
#include "fs/fs.h"
#include "fs/ext2.h"
@@ -27,11 +25,15 @@ static uint32_t next_fifo=0;
term_out screen;
terminal_tty tty1;
+
+/// there also is task_fork, task_wait, task_exit.. which is in scheduler.c
+////////////////////////////////////////
+
int syscall_unhandled(int nr)
{
char msg[256];
tfp_sprintf(msg, "unhandled syscall : %d",nr);
- kpanic(msg);
+ kpanic("%s",msg);
}
int syscall_gettimeofday(struct timeval *tv, struct timezone *tz)
@@ -205,7 +207,6 @@ int syscall_execve(char *name, char **argv, char **env)
uint32_t alloc;
uint32_t entry_global=load_elf(name,&alloc);
-
task_set_brk(alloc);
if(!entry_global)
diff --git a/kernel/usermode.c b/kernel/usermode.c
index b6d01f3..1c039cb 100644
--- a/kernel/usermode.c
+++ b/kernel/usermode.c
@@ -34,6 +34,7 @@ void initfunc()
c2++;
}
}
+
void userfunc()
{
@@ -43,7 +44,10 @@ void userfunc()
// if we are pid 0, replace ourselves with /bin/init TODO: switch to usermode before!
if(task_get_current_pid()==0)
{
- usermode(&initfunc);
+ uint32_t alloc;
+ uint32_t entry_global=load_elf(BIN_INIT,&alloc);
+ task_set_brk(alloc);
+ usermode(entry_global);
}
// kernel worker thread on pid1
diff --git a/kernel/vmem.c b/kernel/vmem.c
index 76421d0..55b9ca3 100644
--- a/kernel/vmem.c
+++ b/kernel/vmem.c
@@ -7,15 +7,12 @@
#include "mem.h"
#include "vmem.h"
-
-
static uint32_t kernel_pages;
static uint32_t fb_addr;
// TODO : why is the frame not 0xfffff??
enum PAGE_PTE_FLAGS
{
-
I86_PTE_PRESENT = 1, //0000000000000000000000000000001
I86_PTE_WRITABLE = 2, //0000000000000000000000000000010
I86_PTE_USER = 4, //0000000000000000000000000000100
@@ -31,7 +28,6 @@ enum PAGE_PTE_FLAGS
enum PAGE_PDE_FLAGS
{
-
I86_PDE_PRESENT = 1, //0000000000000000000000000000001
I86_PDE_WRITABLE = 2, //0000000000000000000000000000010
I86_PDE_USER = 4, //0000000000000000000000000000100
@@ -62,15 +58,12 @@ void pt_entry_del_attrib (pt_entry* e, uint32_t attrib)
}
void pt_entry_set_frame (pt_entry* e , physical_addr addr)
{
- // *e = (*e & ~I86_PTE_FRAME) | addr;
-
*e|=I86_PTE_FRAME&addr;
-
}
+
physical_addr pt_entry_get_frame (pt_entry* e)
{
return *e&I86_PTE_FRAME;
-
}
bool pt_entry_is_present (pt_entry e)
{
@@ -87,93 +80,86 @@ physical_addr pt_entry_pfn (pt_entry e)
return e&I86_PTE_FRAME;
}
-void pd_entry_add_attrib (pd_entry* e, uint32_t attrib)
+void pd_entry_add_attrib (pd_entry* e, uint32_t attrib)
{
*e|=attrib;
}
-void pd_entry_del_attrib (pd_entry* e, uint32_t attrib)
+void pd_entry_del_attrib (pd_entry* e, uint32_t attrib)
{
*e&=~attrib;
}
-void pd_entry_set_frame (pd_entry* e, physical_addr add)
+
+void pd_entry_set_frame (pd_entry* e, physical_addr add)
{
*e|=I86_PDE_FRAME&add;
}
+
physical_addr pd_entry_get_frame (pd_entry* e)
{
return *e&I86_PDE_FRAME;
-
}
-bool pd_entry_is_present (pd_entry e)
+boolpd_entry_is_present (pd_entry e)
{
return e&I86_PDE_PRESENT;
}
+
/*
-bool pd_entry_is_user (pd_entry e)
+bool pd_entry_is_user (pd_entry e)
{
return 1;
}
-bool pd_entry_is_4mb (pd_entry e)
+bool pd_entry_is_4mb (pd_entry e)
{
return 1;
}
*/
-bool pd_entry_is_writable (pd_entry e)
+bool pd_entry_is_writable (pd_entry e)
{
return e&I86_PDE_WRITABLE;
}
-physical_addr pd_entry_pfn (pd_entry e)
+physical_addr pd_entry_pfn (pd_entry e)
{
return e&I86_PDE_FRAME;
}
/*
-void pd_entry_enable_global (pd_entry e)
+void pd_entry_enable_global (pd_entry e)
{
}
*/
-
uint8_t vmmngr_alloc_page (pt_entry* e)
{
-
- //! allocate a free physical frame
- void* p = pmmngr_alloc_block ();
- if (!p)
- return 0;
-
- //! map it to the page
- pt_entry_set_frame (e, (physical_addr)p);
- pt_entry_add_attrib (e, I86_PTE_PRESENT);
-
- return 1;
+ // allocate a free physical frame
+ void* p = pmmngr_alloc_block ();
+ if (!p) return 0;
+
+ // map it to the page
+ pt_entry_set_frame (e, (physical_addr)p);
+ pt_entry_add_attrib (e, I86_PTE_PRESENT);
+
+ return 1;
}
void vmmngr_free_page (pt_entry* e)
{
void* p = (void*)pt_entry_pfn (*e);
- if (p)
- pmmngr_free_block (p);
-
+ if (p) pmmngr_free_block (p);
pt_entry_del_attrib (e, I86_PTE_PRESENT);
}
pt_entry* vmmngr_ptable_lookup_entry (ptable* p, virtual_addr addr)
{
-
- if (p)
- return &p->m_entries[ PAGE_TABLE_INDEX (addr) ];
-
+ if (p) return &p->m_entries[ PAGE_TABLE_INDEX (addr) ];
return 0;
}
-
-
+/// SHIT BELOW THIs LINE
void vmem_free_dir(pdirectory *dir)
{
x86_paging_disable();
@@ -239,6 +225,7 @@ void vmem_free_dir(pdirectory *dir)
//
// TODO: FIX
// KERNEL SPACE HARDCODED TO 5 first PAGES
+// FRAMEBUFER WE GET ON INIT
// PROGRAMM SPACE HARDCODED TO 0x8000000+2 pages and 0x8c00000+1 pages
//
@@ -389,7 +376,7 @@ pdirectory* vmem_new_space_dir(pdirectory *copy_dir)
if(copy_dir!=NULL)
{
uint32_t addr_old=pt_entry_get_frame(&oldtable->m_entries[PAGE_TABLE_INDEX(virt)]);
- klog("physical memcpy(0x%08X,0x%08X,4096)",phys_addr, addr_old);
+ //klog("physical memcpy(0x%08X,0x%08X,4096)",phys_addr, addr_old);
memcpy(phys_addr,addr_old,4096);
}
@@ -448,7 +435,7 @@ pdirectory* vmem_new_space_dir(pdirectory *copy_dir)
if(copy_dir!=NULL)
{
uint32_t addr_old=pt_entry_get_frame(&oldtable->m_entries[PAGE_TABLE_INDEX(virt)]);
- klog("physical memcpy(0x%08X,0x%08X,4096)",phys_addr, addr_old);
+ //klog("physical memcpy(0x%08X,0x%08X,4096)",phys_addr, addr_old);
memcpy(phys_addr,addr_old,4096);
}
@@ -495,3 +482,5 @@ pdirectory* vmem_init(uint32_t kernel_blocks, uint32_t frameb_addr)
kernel_pages=kernel_blocks/1024+1;
return vmem_new_space_dir(NULL);
}
+
+