summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'kernel')
-rw-r--r--kernel/config.h14
-rw-r--r--kernel/fd.c32
-rw-r--r--kernel/fd.h30
-rw-r--r--kernel/fifo.c25
-rw-r--r--kernel/fifo.h11
-rw-r--r--kernel/kernel.c22
-rw-r--r--kernel/kernel.h2
-rw-r--r--kernel/kmalloc.c12
-rw-r--r--kernel/kmalloc.h6
-rw-r--r--kernel/ringbuffer.c21
-rw-r--r--kernel/ringbuffer.h3
-rw-r--r--kernel/smp.c4
-rw-r--r--kernel/spinlock.c20
-rw-r--r--kernel/spinlock.h2
-rw-r--r--kernel/syscalls.c21
-rw-r--r--kernel/usermode.c20
-rw-r--r--kernel/x86.c33
-rw-r--r--kernel/x86.h23
18 files changed, 183 insertions, 118 deletions
diff --git a/kernel/config.h b/kernel/config.h
index 6c9e3d5..2d64db1 100644
--- a/kernel/config.h
+++ b/kernel/config.h
@@ -1,13 +1,16 @@
/********************************************
- * Fool OS Central Configuration File *
+ * F00l 0S Central Configuration File *
********************************************/
-#include "lib/logger/log.h"
-
#ifndef FOOLOS_CONFIG_H
#define FOOLOS_CONFIG_H
+#include "lib/logger/log.h"
+
+#define KERNEL_VERSION "FoolOS 0.3.2"
+#define FIFO_MAX_RINGBUFFERS 20
+
#define FOOLOS_CONSOLE_AUTOBREAK // add newline automatically at end of line
#define FOOLOS_LOG_OFF // do not log anything
@@ -20,5 +23,10 @@
#define LOG_BUF_SIZE 4069
#define LOG_SYSCALLS
+#define BIN_INIT "/bin/init"
+
+#define KMALLOC_MEM_SIZE 1024*1024*8 // 8MB for in kernel-memory
+#define NUMBER_SPINLOCKS 16
+
#endif
diff --git a/kernel/fd.c b/kernel/fd.c
new file mode 100644
index 0000000..5eb2678
--- /dev/null
+++ b/kernel/fd.c
@@ -0,0 +1,32 @@
+#include "fd.h"
+
+bool fd_write(fd* f,uint8_t c)
+{
+ return f->write(f->data,c);
+}
+
+uint8_t fd_read(fd* f)
+{
+ return f->read(f->data);
+}
+
+bool fd_has(fd* f)
+{
+ return f->has(f->data);
+}
+
+bool fd_close(fd* f)
+{
+ return f->close(f->data);
+}
+
+fd fd_from_fifo(fifo* fif)
+{
+ fd f;
+ f.data=fif;
+ f.read=fd_read;
+ f.write=fd_write;
+ f.close=fd_close;
+ f.has=fd_has;
+ return f;
+}
diff --git a/kernel/fd.h b/kernel/fd.h
new file mode 100644
index 0000000..fe048f4
--- /dev/null
+++ b/kernel/fd.h
@@ -0,0 +1,30 @@
+// SIMPLE FILE DESCRIPTOR //
+
+#ifndef FD_H
+#define FD_H
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "fifo.h"
+
+typedef struct fd_struct
+{
+ bool (*write)(struct fd_struct*,uint8_t);
+ uint8_t (*read)(struct fd_struct*);
+ bool (*has)(struct fd_struct*);
+ bool (*close)(struct fd_struct*);
+
+ void *data; // opaque data
+
+}fd;
+
+uint8_t fd_read(fd*);
+bool fd_has(fd*);
+bool fd_write(fd*,uint8_t);
+bool fd_close(fd*);
+
+fd fd_from_fifo(fifo* f);
+//fd fd_from_path(char *path);
+
+#endif
diff --git a/kernel/fifo.c b/kernel/fifo.c
index 314e9ff..eb477be 100644
--- a/kernel/fifo.c
+++ b/kernel/fifo.c
@@ -1,11 +1,21 @@
+#define FOOLOS_MODULE_NAME "fifo"
#include "fifo.h"
+#include "ringbuffer.h"
+#include "config.h"
+#include "lib/logger/log.h"
+
+#include <stddef.h>
+
+static ringbuffer fifo_ringbuffers[FIFO_MAX_RINGBUFFERS];
+static ringbuffer_c=0;
+
bool fifo_put(fifo* f,uint8_t c)
{
return f->put(f->data,c);
}
-volatile uint8_t fifo_get(fifo* f)
+uint8_t fifo_get(fifo* f)
{
return f->get(f->data);
}
@@ -14,3 +24,16 @@ bool fifo_has(fifo* f)
{
return f->has(f->data);
}
+
+fifo fifo_create_buffered(uint8_t size)
+{
+ if (ringbuffer_c>=FIFO_MAX_RINGBUFFERS) panic(FOOLOS_MODULE_NAME,"ran out of ringbuffers for fifos");
+
+ fifo f;
+ fifo_ringbuffers[ringbuffer_c]=ringbuffer_init(size);
+ f.data=&fifo_ringbuffers[ringbuffer_c];
+ f.put=ringbuffer_put;
+ f.get=ringbuffer_get;
+ f.has=ringbuffer_has;
+ return f;
+}
diff --git a/kernel/fifo.h b/kernel/fifo.h
index a574a25..b5c11a4 100644
--- a/kernel/fifo.h
+++ b/kernel/fifo.h
@@ -1,4 +1,4 @@
-// SIMPLE FIFO DRIVER
+// SIMPLE FIFO DRIVER //
#ifndef FIFO_H
#define FIFO_H
@@ -12,12 +12,13 @@ typedef struct fifo_struct
uint8_t (*get)(struct fifo_struct*);
bool (*has)(struct fifo_struct*);
- void *data; // opaque! can be a vt52 or a ringbuffer..
+ void *data; // opaque data
}fifo;
-volatile bool fifo_put(fifo*,uint8_t);
-volatile uint8_t fifo_get(fifo*);
-volatile bool fifo_has(fifo*);
+bool fifo_put(fifo*,uint8_t);
+uint8_t fifo_get(fifo*);
+bool fifo_has(fifo*);
+fifo fifo_create_buffered(uint8_t size);
#endif
diff --git a/kernel/kernel.c b/kernel/kernel.c
index 2760e8b..fca6fe6 100644
--- a/kernel/kernel.c
+++ b/kernel/kernel.c
@@ -1,16 +1,12 @@
#define FOOLOS_MODULE_NAME "kernel"
-
-#ifdef __linux__
-#error "Watchout! this is not Linux but FoolOS. Use a cross-compiler"
-#endif
-
#include "kernel.h"
#include <stdint.h>
+#include <stddef.h>
+
#include "config.h"
#include "types.h"
#include "lib/logger/log.h"
-
#include "fifo.h"
#include "timer.h"
#include "mem.h"
@@ -19,27 +15,19 @@
#include "interrupts.h"
#include "multiboot.h"
#include "ringbuffer.h"
-
-#include <stddef.h>
-
-// for built-in shell
#include "task.h"
#include "multiboot.h"
-
#include "terminal/terminal.h"
#include "driver/screen.h"
-//
-// The Foolish structure of Fool OS!
-//
+// The Foolish structure of Fool OS! //
static fool_os foolos;
fool_os *get_fool()
{
return &foolos;
}
-//
-//
+//
// stdio init : TODO: move away!
static terminal_tty tty1;
@@ -87,6 +75,8 @@ static void init_stdio()
keyboard_init(put_kb);
}
+
+/* F00L 0S Entry point (called directly from asm/multiboot.asm */
void kernel_main(uint32_t eax,uint32_t ebx)
{
//
diff --git a/kernel/kernel.h b/kernel/kernel.h
index 749125e..cb9810c 100644
--- a/kernel/kernel.h
+++ b/kernel/kernel.h
@@ -1,8 +1,6 @@
#ifndef FOOLOS_KERNEL_H
#define FOOLOS_KERNEL_H
-#define KERNEL_VERSION "FoolOS 0.3.1"
-
#include "fifo.h"
#include "terminal/terminal.h"
diff --git a/kernel/kmalloc.c b/kernel/kmalloc.c
index e3b0863..6db07a1 100644
--- a/kernel/kmalloc.c
+++ b/kernel/kmalloc.c
@@ -1,12 +1,10 @@
#define FOOLOS_MODULE_NAME "kmalloc"
#include "kmalloc.h"
+#include "kernel/config.h"
#include "lib/logger/log.h"
-// 8MB for in kernel-memory
-#define MEM_SIZE 1024*1024*8
-
-static uint8_t data[MEM_SIZE]; // bytes
+static uint8_t data[KMALLOC_MEM_SIZE]; // bytes
static uint32_t next;
static uint32_t first;
@@ -41,12 +39,12 @@ uint32_t kballoc(uint32_t size)
uint32_t old=next;
next+=size;
- if(next>=first+MEM_SIZE)
+ if(next>=first+KMALLOC_MEM_SIZE)
{
- panic(FOOLOS_MODULE_NAME,"kballoc ran out of memory! maybe increase MEM_SIZE in kmalloc.c?");
+ panic(FOOLOS_MODULE_NAME,"kballoc ran out of memory! maybe increase KMALLOC_MEM_SIZE in kmalloc.c?");
}
- log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"(%d) : 0x%08X (~%dKB left)",size,old,(MEM_SIZE-next+first)/1024);
+ log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"(%d) : 0x%08X (~%dKB left)",size,old,(KMALLOC_MEM_SIZE-next+first)/1024);
return old;
}
diff --git a/kernel/kmalloc.h b/kernel/kmalloc.h
index 2faecb8..6e7157a 100644
--- a/kernel/kmalloc.h
+++ b/kernel/kmalloc.h
@@ -1,14 +1,12 @@
-#include <stdint.h>
-
/*
* Kernel Block Memory Allocation
*
* The kernel reserves some memory for internal operation, since malloc
* is not available.
- *
- * Last Revision: 17 AUG 2018
*/
+#include <stdint.h>
+
// Allocate size*4096 bytes and returns a 32-bit pointer
uint32_t kballoc (uint32_t size);
diff --git a/kernel/ringbuffer.c b/kernel/ringbuffer.c
index 181679a..31185dc 100644
--- a/kernel/ringbuffer.c
+++ b/kernel/ringbuffer.c
@@ -6,8 +6,7 @@
// TODO: this is disabled because a kb interrupt can occur anytime
// and the kernel will need to access the ringbuffer while we are accessing!
// DO WE need a spinlock in general? do not use a global one anyway!!!!
-
-static int sl=9;
+//static int sl=9;
ringbuffer ringbuffer_init(uint32_t size)
{
@@ -16,19 +15,15 @@ ringbuffer ringbuffer_init(uint32_t size)
f.size=size*4096;
f.front=f.size-1;
f.back=f.size-1;
- f.front=f.size-1;
- f.back=f.size-1;
return f;
}
-volatile bool ringbuffer_put(ringbuffer* f,uint8_t c)
+bool ringbuffer_put(ringbuffer* f,uint8_t c)
{
x86_int_disable();
- lock_spin(sl);
if((f->back-1+f->size)%f->size==f->front)
{
- lock_release(sl);
x86_int_enable();
return false;
}
@@ -38,36 +33,29 @@ volatile bool ringbuffer_put(ringbuffer* f,uint8_t c)
f->back+=f->size;
f->back%=f->size;
- lock_release(sl);
x86_int_enable();
return true;
}
-volatile bool ringbuffer_has(ringbuffer* f)
+bool ringbuffer_has(ringbuffer* f)
{
x86_int_disable();
bool res=true;
- lock_spin(sl);
-
if(f->front==f->back)
res=false;
- lock_release(sl);
x86_int_enable();
return res;
}
-volatile uint8_t ringbuffer_get(ringbuffer* f) // non blocking . please check first
+uint8_t ringbuffer_get(ringbuffer* f) // non blocking . please check first
{
x86_int_disable();
char c;
- lock_spin(sl);
-
if(f->front==f->back)
{
- lock_release(sl);
x86_int_enable();
return ' ';
}
@@ -78,7 +66,6 @@ volatile uint8_t ringbuffer_get(ringbuffer* f) // non blocking . please check fi
f->front+=f->size;
f->front%=f->size;
- lock_release(sl);
x86_int_enable();
return c;
}
diff --git a/kernel/ringbuffer.h b/kernel/ringbuffer.h
index c79fdcf..bb2b875 100644
--- a/kernel/ringbuffer.h
+++ b/kernel/ringbuffer.h
@@ -6,6 +6,7 @@
// Simple FIRST IN FIRST OUT
// requires kballoc - block allocation
+
typedef volatile struct ringbuffer_struct
{
uint32_t size;
@@ -16,7 +17,7 @@ typedef volatile struct ringbuffer_struct
}ringbuffer;
-// create new fifo of given size (in blocks)
+// create new fifo/ringbuffer of given size (in blocks)
ringbuffer ringbuffer_init(uint32_t blocks);
// true on success
diff --git a/kernel/smp.c b/kernel/smp.c
index 2bf0755..08af652 100644
--- a/kernel/smp.c
+++ b/kernel/smp.c
@@ -69,8 +69,10 @@ void kernel_ap()
lock_spin(0);
if(cpu_counter[p]%1000000==0)log(FOOLOS_MODULE_NAME,FOOLOS_LOG_DEBUG,"cpu[%d] %d",p,cpu_counter[p]);
lock_release(0);
+
}
}
+
void smp_log_procdata(smp_processors *procdata)
{
log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"---- smp -----");
@@ -81,7 +83,6 @@ void smp_log_procdata(smp_processors *procdata)
}
-
// this will start all our application processors!
void smp_start_aps(smp_processors *pros,char *path)
{
@@ -124,7 +125,6 @@ void smp_start_aps(smp_processors *pros,char *path)
// start proc 0x7 = 0x7000; etc..
*reg=(6<<8)|(1<<14)|0x7; // 110 SIPI
}
-
}
diff --git a/kernel/spinlock.c b/kernel/spinlock.c
index a8bcc85..68e64a9 100644
--- a/kernel/spinlock.c
+++ b/kernel/spinlock.c
@@ -1,13 +1,14 @@
#define FOOLOS_MODULE_NAME "spinlock"
+#include "spinlock.h"
+
+#include "config.h"
#include "lib/logger/log.h"
-#include "spinlock.h"
#include "x86.h"
// https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html
-#define NUMBER_SPINLOCKS 16
-static spinlock spinlocks[NUMBER_SPINLOCKS];
+static volatile uint32_t spinlocks[NUMBER_SPINLOCKS];
void check_spinlocks()
{
@@ -16,18 +17,17 @@ void check_spinlocks()
log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"%d",spinlocks[i]);
}
-void lock_spin(spinlock i)
+void lock_spin(uint32_t i)
{
- spinlock *addr=spinlocks+i;
-// log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"locking %d (0x%08X)",i,addr);
-
+ uint32_t *addr=spinlocks+i;
+// log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"locking %d (0x%08X)",i,addr);
while(x86_xchg(addr,1));
}
-void lock_release(spinlock i)
+void lock_release(uint32_t i)
{
-// log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"unlocking %d",i);
- spinlock *addr=spinlocks+i;
+// log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"unlocking %d",i);
+ uint32_t *addr=spinlocks+i;
asm("movb $0,%0"::"m"(*addr));
}
diff --git a/kernel/spinlock.h b/kernel/spinlock.h
index 40d9194..df35e3b 100644
--- a/kernel/spinlock.h
+++ b/kernel/spinlock.h
@@ -3,8 +3,6 @@
#include <stdint.h>
-typedef volatile uint8_t spinlock;
-
void lock_spin(spinlock);
void lock_release(spinlock);
diff --git a/kernel/syscalls.c b/kernel/syscalls.c
index c507a4e..9f10d20 100644
--- a/kernel/syscalls.c
+++ b/kernel/syscalls.c
@@ -175,10 +175,22 @@ int syscall_execve(char *name, char **argv, char **env)
while(argv[arg_count]!=NULL)arg_count++;
char **argv1=kballoc(1);
- char **env1=kballoc(1);
+ if(argv!=NULL)
+ {
+ copy_args(argv,argv1);
+ }
+ else{
+ argv1=NULL;
+ }
- copy_args(argv,argv1);
- copy_args(env,env1);
+ char **env1=kballoc(1);
+ if(env!=NULL)
+ {
+ copy_args(env,env1);
+ }
+ else{
+ env1=NULL;
+ }
uint32_t alloc;
uint32_t entry_global=load_elf(name,&alloc);
@@ -214,12 +226,13 @@ int syscall_execve(char *name, char **argv, char **env)
}
+// TODO: support other files too (not only fifo pipes)
int syscall_open(char *name, int flags, int mode)
{
#ifdef LOG_SYSCALLS
log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"open (name=0x%08X(\"%s\"), flags=%d, mode=%d)",name, name,flags,mode);
#endif
- panic(FOOLOS_MODULE_NAME,"unhandled syscall: open");
+ //panic(FOOLOS_MODULE_NAME,"unhandled syscall: open");
}
diff --git a/kernel/usermode.c b/kernel/usermode.c
index db4ac9f..2455ba6 100644
--- a/kernel/usermode.c
+++ b/kernel/usermode.c
@@ -4,6 +4,10 @@
#include "syscalls.h"
#include "kmalloc.h"
+#include "asm/syscall.h"
+#include "asm/usermode.h"
+#include "kernel/config.h"
+
#include "lib/logger/log.h"
#include <stddef.h>
@@ -22,17 +26,15 @@ void install_tss(int cpu_no){
// sys_tss.iomap = ( unsigned short ) sizeof( tss_struct );
}
-void switch_to_user_mode()
+// THIS IS THE FUNCTION TO BE RUN IN RING 3 // USER MODE
+static void userfunc()
{
- asm_usermode();
+ syscall(SYSCALL_EXECVE,BIN_INIT,NULL,NULL);
+ while(1); // we should never get here.
}
-char *argv_init[]={"/bin/init",NULL};
-char *env_init[]={"var1=dupa","var2=mundl",NULL};
-
-// THIS WILL BE RUN IN RING 3!
-void userfunc()
+void switch_to_user_mode()
{
- syscall(SYSCALL_EXECVE,"/bin/init",argv_init,env_init);
- while(1); // we should never get here.
+ usermode(&userfunc);
}
+
diff --git a/kernel/x86.c b/kernel/x86.c
index fdfb638..d664657 100644
--- a/kernel/x86.c
+++ b/kernel/x86.c
@@ -8,16 +8,6 @@
// suffix (b, w, l, q for byte, word, dword, and qword).
//
-
-/*
-volatile void sleep(int i)
-{
- uint64_t clock=timer_get_ticks();
- while(clock+i>timer_get_ticks());
-}
-*/
-
-
// disable interrupts
void x86_int_disable()
{
@@ -49,10 +39,6 @@ uint32_t x86_get_cr1()
return cr;
}
-void x86_mov(uint8_t val)
-{
-}
-
uint32_t x86_get_cr2()
{
uint32_t cr;
@@ -73,36 +59,36 @@ uint32_t x86_get_cr4()
asm volatile("mov %%cr4, %0": "=b"(cr));
return cr;
}
-void x86_outb(int port, uint8_t data)
+void x86_outb(uint32_t port, uint8_t data)
{
asm volatile("outb %0,%w1" : : "a" (data), "d" (port));
}
-uint8_t x86_inb(int port)
+uint8_t x86_inb(uint32_t port)
{
uint8_t data;
asm volatile("inb %w1,%0" : "=a" (data) : "d" (port));
return data;
}
-void x86_outw(int port, uint16_t data)
+void x86_outw(uint32_t port, uint16_t data)
{
asm volatile("outw %0,%w1" : : "a" (data), "d" (port));
}
-uint16_t x86_inw(int port)
+uint16_t x86_inw(uint32_t port)
{
uint16_t data;
asm volatile("inw %w1,%0" : "=a" (data) : "d" (port));
return data;
}
-void x86_outl(int port, uint32_t data)
+void x86_outl(uint32_t port, uint32_t data)
{
asm volatile("outl %0,%w1" : : "a" (data), "d" (port));
}
-uint32_t x86_inl(int port)
+uint32_t x86_inl(uint32_t port)
{
uint32_t data;
asm volatile("inl %w1,%0" : "=a" (data) : "d" (port));
@@ -124,7 +110,6 @@ void x86_paging_enable()
asm volatile("mov %0, %%cr0":: "b"(cr0));
}
-
// disable PT bit in CR0
void x86_paging_disable()
{
@@ -150,10 +135,6 @@ uint8_t x86_xchg(uint8_t *addr, uint8_t val)
"1" (val) :
"cc");
- //log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"xchg val:%d with addr:0x%08X : result: %d",val,addr,result);
-
+ //log(FOOLOS_MODULE_NAME,FOOLOS_LOG_INFO,"xchg val:%d with addr:0x%08X : result: %d",val,addr,result);
return result;
}
-
-
-
diff --git a/kernel/x86.h b/kernel/x86.h
index 2cd9e8e..b634722 100644
--- a/kernel/x86.h
+++ b/kernel/x86.h
@@ -5,25 +5,30 @@
// http://wiki.osdev.org/Interrupt_Service_Routines
-//void sleep(int i); // TODO : Real sleep!
-
-void x86_outb(int port, uint8_t data);
-uint8_t x86_inb(int port);
-void x86_outw(int port, uint16_t data);
-uint16_t x86_inw(int port);
-void x86_outl(int port, uint32_t data);
-uint32_t x86_inl(int port);
+// TODO : Real sleep()!
+
+void x86_outb(uint32_t port, uint8_t data);
+uint8_t x86_inb(uint32_t port);
+
+void x86_outw(uint32_t port, uint16_t data);
+uint16_t x86_inw(uint32_t port);
+
+void x86_outl(uint32_t port, uint32_t data);
+uint32_t x86_inl(uint32_t port);
+
void x86_set_pdbr(uint32_t addr);
void x86_paging_enable();
void x86_flush_tlb(uint32_t addr);
+
void x86_int_enable();
void x86_int_disable();
+
uint32_t x86_get_cr0();
uint32_t x86_get_cr1();
uint32_t x86_get_cr2();
uint32_t x86_get_cr3();
uint32_t x86_get_cr4();
+
uint8_t x86_xchg(uint8_t *addr, uint8_t val);
-void sleep(int i);
#endif