Merge branch 'master' of hackerspace.pl:q3k/Cucumber

Conflicts:
	Kernel/src/Tier0/kmain.c
alentours-dev
q3k 2012-10-29 21:37:32 +01:00
commit da16e53c86
26 changed files with 1213 additions and 391 deletions

1
.gitignore vendored
View File

@ -12,3 +12,4 @@ obj
dump
*~
*gch
tags

View File

@ -22,11 +22,17 @@ LD:=$(ENV)/$(TARGET)-ld
# -O2 sets -foptimize-sibling-calls which breaks code...
CFLAGS:=-m64 -mcmodel=large -Wall -Werror -nostdlib -nostartfiles -nodefaultlibs -std=c99 -g
CFLAGS+=-I ./include -Wno-packed-bitfield-compat -O2 -fno-optimize-sibling-calls -mcmodel=kernel
CFLAGS+= -mno-red-zone -ffreestanding
CFLAGS+= -mno-red-zone -ffreestanding -I ./include/Lua -I ./src/Lua
#CXFLAGS:= -Wall -Werror -nostdlib -fno-builtin -nostartfiles -I ./include
#CXFLAGS+= -nodefaultlibs -fno-exceptions -fno-rtti -fno-stack-protector
#CXFLAGS+= -Wno-packed-bitfield-compat -O3 -mcmodel=kernel
CXFLAGS:=-m64 -mcmodel=large -Wall -Werror -nostdlib -nostartfiles -nodefaultlibs -g
CXFLAGS+=-I ./include -Wno-packed-bitfield-compat -O2 -fno-optimize-sibling-calls -mcmodel=kernel
CXFLAGS+= -mno-red-zone -ffreestanding -I ./include/Lua -I ./src/Lua -fno-exceptions -fno-rtti
CXFLAGS+= -fno-stack-protector
CXFLAGS:= -Wall -Werror -nostdlib -fno-builtin -nostartfiles -I ./include
CXFLAGS+= -nodefaultlibs -fno-exceptions -fno-rtti -fno-stack-protector
CXFLAGS+= -Wno-packed-bitfield-compat -O3 -mcmodel=kernel
LFLAGS:=-nostdlib -nostartfiles -nodefaultlibs
.PHONY: all clean kernel.bin emulate hdd.img
@ -60,12 +66,12 @@ TIER0OBJ := $(patsubst %.asm,%.nao,$(TIER0OBJ))
TIER0 := $(foreach i, $(TIER0OBJ), obj/$(i))
Tier0: $(TIER0)
#TIER1SRC := $(shell find src/Tier1 -mindepth 1 -maxdepth 3 -name "*.cpp")
#TIER1SRC += $(shell find src/Tier1 -mindepth 1 -maxdepth 3 -name "*.asm")
#TIER1OBJ := $(patsubst %.cpp,%.xo,$(TIER1SRC))
#TIER1OBJ := $(patsubst %.asm,%.nao,$(TIER1OBJ))
#TIER1 := $(foreach i, $(TIER1OBJ), obj/$(i))
#Tier1: $(TIER1)
TIER1SRC := $(shell find src/Tier1 -mindepth 1 -maxdepth 3 -name "*.cpp")
TIER1SRC += $(shell find src/Tier1 -mindepth 1 -maxdepth 3 -name "*.asm")
TIER1OBJ := $(patsubst %.cpp,%.xo,$(TIER1SRC))
TIER1OBJ := $(patsubst %.asm,%.nao,$(TIER1OBJ))
TIER1 := $(foreach i, $(TIER1OBJ), obj/$(i))
Tier1: $(TIER1)
src/Lua/liblua.a:
@echo "[i] Building Lua..."
@ -74,9 +80,9 @@ src/Lua/liblua.a:
LUA := src/Lua/liblua.a
Lua: $(LUA)
kernel.bin: version-gen Tier0 Lua
kernel.bin: version-gen Tier0 Tier1 Lua
@echo "[i] Linking kernel.bin..."
@$(LD) -T src/kernel.ld -o kernel.bin $(TIER0) $(TIER1) $(LUA)
@$(LD) -T src/kernel.ld -o kernel.bin $(TIER0) $(TIER1) -Lsrc/Lua -llua
hdd.img: kernel.bin
@echo "[i] Creating HDD image..."
@ -100,7 +106,7 @@ emulate-nohdd: kernel.bin
@echo "[i] Building loader..."
@pushd ../Loader > /dev/null && $(MAKE) -f Makefile loader.bin && popd > /dev/null
@echo "[i] Starting QEMU..."
@qemu-system-x86_64 -d int -smp 4 -kernel ../Loader/loader.bin -initrd kernel.bin
@qemu-system-x86_64 --no-kvm -d int -smp 4 -kernel ../Loader/loader.bin -initrd kernel.bin
emulate: hdd.img
@echo "[i] Starting QEmu..."

View File

@ -2,8 +2,32 @@
#define __TIER0_ANSI_SETJMP_H__
#include "Tier0/panic.h"
#include "Tier0/interrupts.h"
typedef u32 jmp_buf;
struct __jmp_buf {
u64 rax; // 0
u64 rbx; // 8
u64 rcx; // 16
u64 rdx; // 24
u64 rsi; // 32
u64 rdi; // 40
u64 rsp; // 48
u64 rbp; // 56
u64 r8; // 64
u64 r9; // 72
u64 r10; // 80
u64 r11; // 88
u64 r12; // 96
u64 r13; // 104
u64 r14; // 112
u64 r15; // 120
u64 rip; // 128
} __attribute__((packed));
// POSIX sez:
// jmp_buf must be an array type so that poor, poor programmers don't have
// to use pointers, yet we can still return-by-argument. whaddafuck.
typedef struct __jmp_buf jmp_buf[1];
int setjmp(jmp_buf env);
void longjmp(jmp_buf env, int value);

View File

@ -5,8 +5,9 @@
//int printf(char *format, va_list args);
#define BUFSIZ 1024
#define FILE int
int sprintf(char *str, const char *format, ...);
typedef int FILE;
//int sprintf(char *str, const char *format, ...);
int feof(FILE *stream);
unsigned int fread(void *ptr, unsigned long long int size, unsigned long long int count, FILE *stream);
int getc(FILE *stream);
@ -25,7 +26,7 @@ unsigned long long int fwrite(const void *ptr, unsigned long long int size, unsi
int fseek(FILE *stream, long offset, int whence);
int ftell(FILE *stream);
int fputs(const char *s, FILE *stream);
int printf(const char *format, ...);
#define printf(format, ...) fprintf(stdout, format, ##__VA_ARGS__);
#define EOF -1
extern FILE *stdin;

View File

@ -12,9 +12,9 @@ char *strstr(const char *haystack, const char *needle);
unsigned long long int strspn(const char *s, const char *accept);
void *memchr(const void *s, int c, unsigned long long int n);
char *strcpy(char *dest, const char *src);
int memcmp(const void *s1, const void *s2, unsigned long int n);
#define strcoll(A, B) strcmp(A, B)
#define strlen(A) kstrlen(A)
#define memcpy(A, B, C) kmemcpy(A, B, C)
#define memcmp(A, B, C) kmemcmp((const u8*)A, (const u8*)B, C)
#endif

View File

@ -4,27 +4,27 @@
#include "types.h"
typedef struct {
u32 Value;
u64 Value;
} T_ATOMIC;
#define atomic_init(n) { (n) }
#define atomic_read(v) ((v)->Value)
#define atomic_set(v, n) (((v)->Value) = n)
static inline void atomic_add(T_ATOMIC *Atom, u32 Value)
static inline void atomic_add(T_ATOMIC *Atom, u64 Value)
{
__asm__ volatile("lock addl %1, %0" : "+m"(Atom->Value) : "ir"(Value));
__asm__ volatile("lock addq %1, %0" : "+m"(Atom->Value) : "ir"(Value));
}
static inline void atomic_sub(T_ATOMIC *Atom, u32 Value)
static inline void atomic_sub(T_ATOMIC *Atom, u64 Value)
{
__asm__ volatile("lock subl %1, %0" : "+m"(Atom->Value) : "ir"(Value));
__asm__ volatile("lock subq %1, %0" : "+m"(Atom->Value) : "ir"(Value));
}
static inline u8 atomic_sub_and_test(T_ATOMIC *Atom, u32 Value)
static inline u8 atomic_sub_and_test(T_ATOMIC *Atom, u64 Value)
{
u8 C;
__asm__ volatile("lock subl %2, %0\n"
__asm__ volatile("lock subq %2, %0\n"
"sete %1\n"
: "+m"(Atom->Value), "=qm"(C)
: "ir" (Value) : "memory");
@ -33,18 +33,18 @@ static inline u8 atomic_sub_and_test(T_ATOMIC *Atom, u32 Value)
static inline void atomic_inc(T_ATOMIC *Atom)
{
__asm__ volatile("lock incl %0" : "+m"(Atom->Value));
__asm__ volatile("lock incq %0" : "+m"(Atom->Value));
}
static inline void atomic_dec(T_ATOMIC *Atom)
{
__asm__ volatile("lock decl %0" : "+m"(Atom->Value));
__asm__ volatile("lock decq %0" : "+m"(Atom->Value));
}
static inline u8 atomic_dec_and_test(T_ATOMIC *Atom)
{
u8 C;
__asm__ volatile("lock decl %0\n"
__asm__ volatile("lock decq %0\n"
"sete %1" : "+m"(Atom->Value), "=qm"(C) :: "memory");
return C;
}
@ -52,7 +52,7 @@ static inline u8 atomic_dec_and_test(T_ATOMIC *Atom)
static inline u8 atomic_inc_and_test(T_ATOMIC *Atom)
{
u8 C;
__asm__ volatile("lock incl %0\n"
__asm__ volatile("lock incq %0\n"
"sete %1" : "+m"(Atom->Value), "=qm"(C) :: "memory");
return C;
}

View File

@ -3,7 +3,7 @@
#include "types.h"
#include "Tier1/CLogger.h"
#include "Tier1/CTask.h"
//#include "Tier1/CTask.h"
#define CKERNEL_MAGIC 0x8BA67FE9
@ -19,7 +19,7 @@ namespace cb {
u32 m_dwMagic;
CLogger *m_Logger;
CTask *CreateKernelTask(void);
// CTask *CreateKernelTask(void);
};
};

View File

@ -3,63 +3,11 @@
#include "types.h"
#include "Tier1/CKernel.h"
#include "Tier1/CPageDirectory.h"
//#include "Tier1/CPageDirectory.h"
#include "Tier1/CSemaphore.h"
#include "Tier0/semaphore.h"
// Task memory map...
// _______________________
// 0x00000000 | | This is left unused because of possible
// | thar be dragons | segfaults due to dereferencing of null
// |_______________________| pointers and other crap like that
// 0x10000000 | |
// | process code | The process image lies here, unless
// |_______________________| we're in the kernel, then it's unused
// 0x20000000 | |
// | process heap |
// |_______________________|
// 0x30000000 | |
// | process heap | The process heap is here. Managed via
// |_______________________| the tier0 heap mechanism
// 0x40000000 | |
// | process heap |
// |_______________________|
// 0x50000000 | |
// | process heap |
// |_______________________|
// 0x60000000 | |
// | process data |
// |_______________________| .data, .rodata and the like
// 0x70000000 | |
// | process data |
// |_______________________|
// 0x80000000 | |
// | |
// |_______________________|
// 0x90000000 | |
// | |
// |_______________________|
// 0xA0000000 | | Process stack. Starts at 4kb, then
// | process stack | grows if needed
// |_______________________|
// 0xB0000000 | |
// | |
// |_______________________|
// 0xC0000000 | |
// | kernel code |
// |_______________________| We need this in every process.
// 0xD0000000 | |
// | kernel heap |
// |_______________________|
// 0xE0000000 | |
// | IPC |
// |_______________________| For IPC (message passing)
// 0xF0000000 | |
// | IPC |
// |_______________________|
// 0xFFFFFFFF
#define TASK_MAP_CODE_START 0x10000000
/*#define TASK_MAP_CODE_START 0x10000000
#define TASK_MAP_CODE_SIZE 0x10000000
#define TASK_MAP_HEAP_START 0x20000000
@ -69,10 +17,10 @@
#define TASK_MAP_STACK_SIZE 0x10000000
#define TASK_MAP_KERNEL_START 0xC0000000
#define TASK_MAP_KERNEL_SIZE 0x20000000
#define TASK_MAP_KERNEL_SIZE 0x20000000*/
extern "C" {
u32 ctask_geteip(void);
u64 ctask_getrip(void);
}
namespace cb {
@ -102,27 +50,27 @@ namespace cb {
friend class CScheduler;
protected:
void *m_Owner; //TODO: Replace me with a real type
CPageDirectory *m_Directory;
//CPageDirectory *m_Directory;
ETaskPriority m_Priority;
ETaskRing m_Ring;
bool m_User;
volatile u32 m_PID;
volatile u64 m_PID;
volatile u32 m_ESP, m_EIP, m_EBP;
volatile u64 m_ESP, m_EIP, m_EBP;
volatile ETaskStatus m_Status;
volatile u32 m_StatusData;
volatile u64 m_StatusData;
u32 m_HeapStart;
u32 m_HeapSize;
u64 m_HeapStart;
u64 m_HeapSize;
u32 m_StackStart;
u32 m_StackSize;
u64 m_StackStart;
u64 m_StackSize;
u32 m_ImageStart;
u32 m_ImageSize;
u64 m_ImageStart;
u64 m_ImageSize;
u32 m_KernelStart;
u32 m_KernelSize;
u64 m_KernelStart;
u64 m_KernelSize;
bool m_CreatedStack;
@ -135,29 +83,29 @@ namespace cb {
CTask(bool User = 0, bool Empty = false);
~CTask(void);
CPageDirectory *GetPageDirectory(void);
//CPageDirectory *GetPageDirectory(void);
// Equivalent of the POSIX fork() call.
CTask *Fork(void);
inline u32 GetPID(void) { return m_PID; }
inline u32 GetESP(void) { return m_ESP; }
inline u32 GetEIP(void) { return m_EIP; }
inline u32 GetEBP(void) { return m_EBP; }
inline u64 GetPID(void) { return m_PID; }
inline u64 GetESP(void) { return m_ESP; }
inline u64 GetEIP(void) { return m_EIP; }
inline u64 GetEBP(void) { return m_EBP; }
inline u32 GetPageDirectoryPhysicalAddress(void)
/*inline u64 GetPageDirectoryPhysicalAddress(void)
{
return m_Directory->m_Directory->PhysicalAddress;
}
}*/
inline void SetESP(u32 ESP) { m_ESP = ESP; }
inline void SetEIP(u32 EIP) { m_EIP = EIP; }
inline void SetEBP(u32 EBP) { m_EBP = EBP; }
inline void SetESP(u64 ESP) { m_ESP = ESP; }
inline void SetEIP(u64 EIP) { m_EIP = EIP; }
inline void SetEBP(u64 EBP) { m_EBP = EBP; }
inline void SetPageDirectory(CPageDirectory *Directory)
/*inline void SetPageDirectory(CPageDirectory *Directory)
{
m_Directory = Directory;
}
}*/
void Dump(void);
@ -170,17 +118,17 @@ namespace cb {
void Enable(void);
// Like sleep(). Usually microseconds.
void Sleep(u32 Ticks);
void Sleep(u64 Ticks);
// Used for waking up via a CTimer
static bool WakeUp(u32 Extra);
static bool WakeUp(u64 Extra);
inline ETaskStatus GetStatus(void)
{
return m_Status;
}
inline u32 GetStatusData(void)
inline u64 GetStatusData(void)
{
return m_StatusData;
}

View File

@ -0,0 +1,8 @@
{
"folders":
[
{
"path": "/home/q3k/Cucumber/Kernel"
}
]
}

View File

@ -0,0 +1,565 @@
{
"auto_complete":
{
"selected_items":
[
[
"fno",
"fno-stack-protector"
]
]
},
"buffers":
[
{
"file": "src/Tier1/CLogger.cpp",
"settings":
{
"buffer_size": 927,
"line_ending": "Unix"
}
},
{
"file": "src/Tier1/CKernel.cpp",
"settings":
{
"buffer_size": 2679,
"line_ending": "Unix"
}
},
{
"file": "include/Tier1/CKernel.h",
"settings":
{
"buffer_size": 477,
"line_ending": "Unix"
}
},
{
"file": "src/Tier0/cpp.c",
"settings":
{
"buffer_size": 776,
"line_ending": "Unix"
}
},
{
"file": "include/Tier0/cpp.h",
"settings":
{
"buffer_size": 242,
"line_ending": "Unix"
}
},
{
"file": "src/Tier1/new.cpp",
"settings":
{
"buffer_size": 329,
"line_ending": "Unix"
}
},
{
"file": "Makefile",
"settings":
{
"buffer_size": 4219,
"line_ending": "Unix"
}
},
{
"file": "src/Tier1/CTask.asm",
"settings":
{
"buffer_size": 82,
"line_ending": "Unix"
}
},
{
"file": "src/Tier1/CTask.cpp",
"settings":
{
"buffer_size": 4774,
"line_ending": "Unix"
}
},
{
"file": "include/Tier1/CTask.h",
"settings":
{
"buffer_size": 3787,
"line_ending": "Unix"
}
},
{
"file": "include/Tier0/atomic_operations.h",
"settings":
{
"buffer_size": 1400,
"line_ending": "Unix"
}
}
],
"build_system": "",
"command_palette":
{
"height": 0.0,
"selected_items":
[
],
"width": 0.0
},
"console":
{
"height": 0.0
},
"distraction_free":
{
"menu_visible": true,
"show_minimap": false,
"show_open_files": false,
"show_tabs": false,
"side_bar_visible": false,
"status_bar_visible": false
},
"file_history":
[
"/home/q3k/Cucumber/Kernel/include/Tier0/semaphore.h"
],
"find":
{
"height": 0.0
},
"find_in_files":
{
"height": 0.0,
"where_history":
[
]
},
"find_state":
{
"case_sensitive": false,
"find_history":
[
"u32",
"Semaphore",
"SEMAPHORE",
"semaphore",
"Semaphore",
"SEMAPHORE",
"semaphore",
"Semaphore",
"SEMAPHORE",
"semaphore",
"Semaphore",
"SEMAPHORE",
"semaphore",
"SEMAPHORE",
"include",
"Semaphore",
"SEMAPHORE",
"semaphore",
"Semaphore",
"SEMAPHORE",
"semaphore",
"Semaphore",
"SEMAPHORE",
"semaphore",
"Semaphore",
"SEMAPHORE",
"semaphore",
"PHO",
"pho",
"PHO",
"pho",
"PHO",
"pho",
"#",
"nic.h\"\n #include \"Tier0/he"
],
"highlight": true,
"in_selection": false,
"preserve_case": false,
"regex": false,
"replace_history":
[
],
"reverse": false,
"show_context": true,
"use_buffer2": true,
"whole_word": false,
"wrap": true
},
"groups":
[
{
"selected": 6,
"sheets":
[
{
"buffer": 0,
"file": "src/Tier1/CLogger.cpp",
"settings":
{
"buffer_size": 927,
"regions":
{
},
"selection":
[
[
298,
298
]
],
"settings":
{
"syntax": "Packages/C++/C++.tmLanguage",
"tab_size": 4,
"translate_tabs_to_spaces": true
},
"translation.x": 0.0,
"translation.y": 0.0,
"zoom_level": 1.0
},
"type": "text"
},
{
"buffer": 1,
"file": "src/Tier1/CKernel.cpp",
"settings":
{
"buffer_size": 2679,
"regions":
{
},
"selection":
[
[
196,
196
]
],
"settings":
{
"syntax": "Packages/C++/C++.tmLanguage",
"tab_size": 4,
"translate_tabs_to_spaces": true
},
"translation.x": 0.0,
"translation.y": 0.0,
"zoom_level": 1.0
},
"type": "text"
},
{
"buffer": 2,
"file": "include/Tier1/CKernel.h",
"settings":
{
"buffer_size": 477,
"regions":
{
},
"selection":
[
[
0,
0
]
],
"settings":
{
"syntax": "Packages/C++/C++.tmLanguage",
"tab_size": 4,
"translate_tabs_to_spaces": true
},
"translation.x": 0.0,
"translation.y": 0.0,
"zoom_level": 1.0
},
"type": "text"
},
{
"buffer": 3,
"file": "src/Tier0/cpp.c",
"settings":
{
"buffer_size": 776,
"regions":
{
},
"selection":
[
[
0,
0
]
],
"settings":
{
"syntax": "Packages/C++/C.tmLanguage",
"tab_size": 4,
"translate_tabs_to_spaces": true
},
"translation.x": 0.0,
"translation.y": 0.0,
"zoom_level": 1.0
},
"type": "text"
},
{
"buffer": 4,
"file": "include/Tier0/cpp.h",
"settings":
{
"buffer_size": 242,
"regions":
{
},
"selection":
[
[
36,
36
]
],
"settings":
{
"syntax": "Packages/C++/C++.tmLanguage"
},
"translation.x": 0.0,
"translation.y": 0.0,
"zoom_level": 1.0
},
"type": "text"
},
{
"buffer": 5,
"file": "src/Tier1/new.cpp",
"settings":
{
"buffer_size": 329,
"regions":
{
},
"selection":
[
[
0,
0
]
],
"settings":
{
"syntax": "Packages/C++/C++.tmLanguage"
},
"translation.x": 0.0,
"translation.y": 0.0,
"zoom_level": 1.0
},
"type": "text"
},
{
"buffer": 6,
"file": "Makefile",
"settings":
{
"buffer_size": 4219,
"regions":
{
},
"selection":
[
[
1308,
1308
]
],
"settings":
{
"syntax": "Packages/Makefile/Makefile.tmLanguage"
},
"translation.x": 0.0,
"translation.y": 90.0,
"zoom_level": 1.0
},
"type": "text"
},
{
"buffer": 7,
"file": "src/Tier1/CTask.asm",
"settings":
{
"buffer_size": 82,
"regions":
{
},
"selection":
[
[
79,
79
]
],
"settings":
{
"syntax": "Packages/Text/Plain text.tmLanguage"
},
"translation.x": 0.0,
"translation.y": 0.0,
"zoom_level": 1.0
},
"type": "text"
},
{
"buffer": 8,
"file": "src/Tier1/CTask.cpp",
"settings":
{
"buffer_size": 4774,
"regions":
{
},
"selection":
[
[
404,
404
]
],
"settings":
{
"syntax": "Packages/C++/C++.tmLanguage",
"tab_size": 4,
"translate_tabs_to_spaces": true
},
"translation.x": 0.0,
"translation.y": 0.0,
"zoom_level": 1.0
},
"type": "text"
},
{
"buffer": 9,
"file": "include/Tier1/CTask.h",
"settings":
{
"buffer_size": 3787,
"regions":
{
},
"selection":
[
[
2983,
2983
]
],
"settings":
{
"syntax": "Packages/C++/C++.tmLanguage",
"tab_size": 4,
"translate_tabs_to_spaces": true
},
"translation.x": 0.0,
"translation.y": 1125.0,
"zoom_level": 1.0
},
"type": "text"
},
{
"buffer": 10,
"file": "include/Tier0/atomic_operations.h",
"settings":
{
"buffer_size": 1400,
"regions":
{
},
"selection":
[
[
1294,
1294
]
],
"settings":
{
"syntax": "Packages/C++/C++.tmLanguage",
"tab_size": 4,
"translate_tabs_to_spaces": true
},
"translation.x": 0.0,
"translation.y": 0.0,
"zoom_level": 1.0
},
"type": "text"
}
]
}
],
"incremental_find":
{
"height": 0.0
},
"input":
{
"height": 31.0
},
"layout":
{
"cells":
[
[
0,
0,
1,
1
]
],
"cols":
[
0.0,
1.0
],
"rows":
[
0.0,
1.0
]
},
"menu_visible": true,
"output.exec":
{
"height": 100.0
},
"replace":
{
"height": 0.0
},
"save_all_on_build": true,
"select_file":
{
"height": 0.0,
"selected_items":
[
],
"width": 0.0
},
"select_project":
{
"height": 0.0,
"selected_items":
[
],
"width": 0.0
},
"show_minimap": true,
"show_open_files": true,
"show_tabs": true,
"side_bar_visible": true,
"side_bar_width": 276.0,
"status_bar_visible": true
}

View File

@ -31,9 +31,9 @@ PLATS= aix ansi bsd cucumber-kernel freebsd generic linux macosx mingw posix sol
LUA_A= liblua.a
CORE_O= lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o \
lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o \
ltm.o lundump.o lvm.o lzio.o snprintf.o glue.o
# removed mathlib, oslib, iolib, loadlib ~q3k
LIB_O= lauxlib.o lbaselib.o lbitlib.o lcorolib.o ldblib.o \
ltm.o lundump.o lvm.o lzio.o snprintf.o glue.o lauxlib.o
# removed mathlib, oslib, iolib, loadlib, ~q3k
LIB_O= lbaselib.o lbitlib.o lcorolib.o ldblib.o \
lstrlib.o ltablib.o linit.o
BASE_O= $(CORE_O) $(LIB_O) $(MYOBJS)

View File

@ -6,8 +6,12 @@
#include "stdio.h"
#include "Tier0/panic.h"
#include "Tier0/kstdio.h"
#include "Tier0/heap.h"
#include "Tier0/kstdlib.h"
#include "Tier0/kstdlib.h"
int errno;
#define NULL 0
// file descriptors for stdio...
FILE _stdin = 0;
@ -38,33 +42,185 @@ FILE *stderr = &_stderr;
// setjmp.h implementation
int setjmp(jmp_buf env)
{
PANIC("setjmp() stub.");
return 0;
// gods help me
u64 Result;
__asm__ __volatile__(
// rax in our buffer is going to be set to the magic value of
// 0, in order to let know code underneath that we are actually
// in the jump target, not the setting function
"movq $0, 0(%%rax)\n"
"movq %%rbx, 8(%%rax)\n"
"movq %%rcx, 16(%%rax)\n"
"movq %%rdx, 24(%%rax)\n"
"movq %%rsi, 32(%%rax)\n"
"movq %%rdi, 40(%%rax)\n"
"movq %%rsp, 48(%%rax)\n"
"movq %%rbp, 56(%%rax)\n"
"movq %%r8, 64(%%rax)\n"
"movq %%r9, 72(%%rax)\n"
"movq %%r10, 80(%%rax)\n"
"movq %%r11, 88(%%rax)\n"
"movq %%r12, 96(%%rax)\n"
"movq %%r13, 104(%%rax)\n"
"movq %%r14, 112(%%rax)\n"
"movq %%r15, 120(%%rax)\n"
"movq %%rax, %%rbx\n"
"call get_rip\n"
// two possibilities with rax here:
// - if not zero, then we're in setjmp for the first time
// - if zero, then we're in setjmp from longjmp
"test %%rax, %%rax\n"
"jnz setjmp_original\n"
// still here? then we just came from longjmp(). let's return the
// argument 'value', which we have on our stack
"popq %%rbx\n"
"popq %%rax\n"
"movq %%rax, %0\n"
"jmp setjmp_done\n"
// helper function (get rip)
"get_rip: movq (%%rsp), %%rax\n ret\n"
// original setting return
// don't forget to set rip!
"setjmp_original:\n"
"movq %%rax, 128(%%rbx)\n"
"mov $0, %0\n"
"setjmp_done:\n"
"nop"
:"=r"(Result):"a"(&env[0]));
/*kprintf("SETJMP! :D\n");
kprintf("%x %x %x %x\n", env[0].rax, env[0].rbx, env[0].rcx, env[0].rdx);
kprintf("%x %x %x %x\n", env[0].rsi, env[0].rdi, env[0].rsp, env[0].rbp);
kprintf("%x %x %x %x\n", env[0].r8, env[0].r9, env[0].r10, env[0].r11);
kprintf("%x %x %x %x\n", env[0].r12, env[0].r13, env[0].r14, env[0].rip);
kprintf("-------------\n");*/
return (int)Result;
}
void longjmp(jmp_buf env, int value)
{
PANIC("longjmp() stub.");
__asm__ __volatile__(
"movq 48(%%rax), %%rsp\n"
// we can now push to the setjmp env stack
// push 'value'
"pushq %%rbx\n"
// push saved rbx
"movq 8(%%rax), %%rbx\n"
"pushq %%rbx\n"
"movq 16(%%rax), %%rcx\n"
"movq 24(%%rax), %%rdx\n"
"movq 32(%%rax), %%rsi\n"
"movq 40(%%rax), %%rdi\n"
"movq 56(%%rax), %%rbp\n"
"movq 64(%%rax), %%r8\n"
"movq 72(%%rax), %%r9\n"
"movq 80(%%rax), %%r10\n"
"movq 88(%%rax), %%r11\n"
"movq 96(%%rax), %%r12\n"
"movq 104(%%rax), %%r13\n"
"movq 112(%%rax), %%r14\n"
"movq 120(%%rax), %%r15\n"
// get rip
"movq 128(%%rax), %%rbx\n"
// set rax to 0 so that setjmp knows we're not the original call
"xorq %%rax, %%rax\n"
// off we go!
"jmp %%rbx\n"
::"a"(&env[0]), "b"(value));
}
// stdio.h implementation
//int printf(char *format, va_list args)
//{
// kprintf("lua :");
//
// va_list duplicate_args;
// va_copy(duplicate_args, args);
// int result = kprintf(format, duplicate_args);
// va_end(duplicate_args);
//
// return result;
//}
// from snprintf.c
int rpl_snprintf(char *str, unsigned long int size, const char *format, ...);
int fprintf(FILE *stream, const char *format, ...)
{
va_list duplicate_args;
va_list args;
va_start(args, format);
va_copy(duplicate_args, args);
char Buffer[1024];
int Result = -1;
if (stream == stdout)
Result = rpl_vsnprintf(Buffer, 1024, format, duplicate_args);
else if (stream == stderr)
{
kprintf("ERR: ");
Result = rpl_vsnprintf(Buffer, 1024, format, duplicate_args);
}
if (Result >= 0)
kprintf("%s", Buffer);
va_end(duplicate_args);
va_end(args);
return Result;
}
int fflush(FILE *stream)
{
return 0;
}
int getc(FILE *stream)
{
PANIC("Lua: getc() stub.");
return 0;
}
int feof(FILE *stream)
{
#warning feof() stub!
return 0;
}
int ferror(FILE *stream)
{
#warning ferror() stub!
return 0;
}
unsigned int fread(void *ptr, unsigned long long int size, unsigned long long int count, FILE *stream)
{
PANIC("Lua: fread() stub.");
return 0;
}
FILE *fopen(const char *path, const char *mode)
{
#warning fopen() stub!
return NULL;
}
FILE *freopen(const char *path, const char *mode, FILE *stream)
{
#warning freopen() stub!
return NULL;
}
int fclose(FILE *fp)
{
#warning fclose() stub!
return 0;
}
// stdlib.h implementation
void *malloc(unsigned long long int i)
{
PANIC("malloc() stub.");
return 0;
return kmalloc(i);
}
int abs(int X)
@ -74,11 +230,49 @@ int abs(int X)
void abort(void)
{
PANIC("abort() stub.");
kprintf("\n\nabort()\n");
for (;;) {}
//PANIC("Lua: abort() called.");
__builtin_unreachable();
}
void *realloc(void *ptr, unsigned long int size)
{
if (size == 0 && ptr != 0)
{
kfree(ptr);
return NULL;
}
if (ptr == NULL)
return kmalloc(size);
void *Data = kmalloc(size);
kmemcpy(Data, ptr, size);
kfree(ptr);
return Data;
}
void free(void *ptr)
{
kfree(ptr);
}
// string.h implementation
int memcmp(const void *s1, const void *s2, unsigned long int n)
{
const unsigned char *p1 = s1, *p2 = s2;
while(n--)
if( *p1 != *p2 )
return *p1 - *p2;
else
{
p1++;
p2++;
}
return 0;
}
const char *strchr (const char *str, int character)
{
while (*str != 0)
@ -103,4 +297,65 @@ char *strpbrk(const char *s1, const char *s2)
return 0;
}
int luai_numpow(void *L, int a, int b)
{
int Result = 1;
for (int i = 0; i < b; i++)
{
Result *= a;
}
return Result;
}
long int strtol (const char *str, char **endptr, int base)
{
const char *beg = str;
// find end of string
while (*str >= '0' && *str <= '9')
str++;
str--;
if (endptr != 0)
*endptr = (char *)str; // shut up gcc
long int value = 0;
while (str >= beg)
{
char v = *str - '0';
value += v;
value *= 10;
str--;
}
return value;
}
char *strerror(int errnum)
{
return "Terrible error.";
}
char *strstr(const char *haystack, const char *needle)
{
unsigned int HaystackLength = kstrlen(haystack);
unsigned int NeedleLength = kstrlen(needle);
if (NeedleLength > HaystackLength)
return NULL;
for (unsigned int i = 0; i <= HaystackLength - NeedleLength; i++)
{
char Okay = 1;
for (unsigned int j = 0; j < NeedleLength; j++)
{
if (*(haystack + i + j) != *(needle + j))
{
Okay = 0;
break;
}
}
if (Okay)
return (char *)haystack + i;
}
return NULL;
}

View File

@ -418,12 +418,12 @@ static int luaB_tostring (lua_State *L) {
static const luaL_Reg base_funcs[] = {
{"assert", luaB_assert},
{"collectgarbage", luaB_collectgarbage},
{"dofile", luaB_dofile},
// {"dofile", luaB_dofile},
{"error", luaB_error},
{"getmetatable", luaB_getmetatable},
{"ipairs", luaB_ipairs},
{"loadfile", luaB_loadfile},
{"load", luaB_load},
// {"loadfile", luaB_loadfile},
// {"load", luaB_load},
#if defined(LUA_COMPAT_LOADSTRING)
{"loadstring", luaB_load},
#endif
@ -431,16 +431,16 @@ static const luaL_Reg base_funcs[] = {
{"pairs", luaB_pairs},
{"pcall", luaB_pcall},
{"print", luaB_print},
{"rawequal", luaB_rawequal},
{"rawlen", luaB_rawlen},
{"rawget", luaB_rawget},
{"rawset", luaB_rawset},
{"select", luaB_select},
// {"rawequal", luaB_rawequal},
// {"rawlen", luaB_rawlen},
// {"rawget", luaB_rawget},
// {"rawset", luaB_rawset},
// {"select", luaB_select},
{"setmetatable", luaB_setmetatable},
{"tonumber", luaB_tonumber},
// {"tonumber", luaB_tonumber},
{"tostring", luaB_tostring},
{"type", luaB_type},
{"xpcall", luaB_xpcall},
// {"xpcall", luaB_xpcall},
{NULL, NULL}
};

View File

@ -28,15 +28,15 @@
*/
static const luaL_Reg loadedlibs[] = {
{"_G", luaopen_base},
{LUA_LOADLIBNAME, luaopen_package},
{LUA_COLIBNAME, luaopen_coroutine},
// {LUA_LOADLIBNAME, luaopen_package},
// {LUA_COLIBNAME, luaopen_coroutine},
{LUA_TABLIBNAME, luaopen_table},
{LUA_IOLIBNAME, luaopen_io},
{LUA_OSLIBNAME, luaopen_os},
{LUA_STRLIBNAME, luaopen_string},
{LUA_BITLIBNAME, luaopen_bit32},
{LUA_MATHLIBNAME, luaopen_math},
{LUA_DBLIBNAME, luaopen_debug},
// {LUA_IOLIBNAME, luaopen_io},
// {LUA_OSLIBNAME, luaopen_os},
// {LUA_STRLIBNAME, luaopen_string},
// {LUA_BITLIBNAME, luaopen_bit32},
// {LUA_MATHLIBNAME, luaopen_math},
// {LUA_DBLIBNAME, luaopen_debug},
{NULL, NULL}
};

View File

@ -211,8 +211,8 @@
*/
#if defined(LUA_LIB) || defined(lua_c)
#include <stdio.h>
#define luai_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
#define luai_writeline() (luai_writestring("\n", 1), fflush(stdout))
#define luai_writestring(s,l) printf("%s", s)
#define luai_writeline() luai_writestring("\n", 1)
#endif
/*
@ -399,6 +399,10 @@
@@ lua_number2str converts a number to a string.
@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.
*/
// lolwut
int rpl_snprintf(char *str, size_t size, const char *format, ...);
#define snprintf rpl_snprintf
#define sprintf(s, ...) snprintf(s, 9001, __VA_ARGS__)
#define LUA_NUMBER_SCAN "%i"
#define LUA_NUMBER_FMT "%i"
#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
@ -427,10 +431,10 @@
/* the following operations need the math library */
#if defined(lobject_c) || defined(lvm_c)
//#include <math.h>
//#define luai_nummod(L,a,b) ((a) - floor((a)/(b))*(b))
#define luai_nummod(L,a,b) ((a) % (b))
//#define luai_numpow(L,a,b) (pow(a,b))
LUA_NUMBER luai_nummod(void *L, LUA_NUMBER a, LUA_NUMBER b);
LUA_NUMBER luai_numpow(void *L, LUA_NUMBER a, LUA_NUMBER b);
#endif
/* these are quite standard operations */

View File

@ -3,15 +3,15 @@
#include "Tier0/cpp.h"
#include "Tier0/kstdio.h"
extern u64 g_start_ctors;
extern u64 g_end_ctors;
extern u64 _start_ctors;
extern u64 _end_ctors;
void CKernelStart(void);
void cpp_call_ctors(void)
{
u32 Number = ((void *)&g_end_ctors - (void *)&g_start_ctors) / 4;
u32 Number = ((void *)&_end_ctors - (void *)&_start_ctors) / 8;
kprintf("[i] Calling %i constructors before jumping to Tier1..\n", Number);
for(u64 *C = (u64*)&g_start_ctors; C < (u64*)&g_end_ctors; ++C)
for(u64 *C = (u64*)&_start_ctors; C < (u64*)&_end_ctors; ++C)
{
((void (*) (void)) (*C)) ();
}

View File

@ -15,7 +15,7 @@
//#include "Tier0/kbd_layout.h"
#include "Tier0/physmem.h"
#include "Tier0/heap.h"
//#include "Tier0/cpp.h"
#include "Tier0/cpp.h"
#include "Tier0/exceptions.h"
#include "Tier0/panic.h"
//#include "Tier0/prng.h"
@ -107,15 +107,14 @@ void kmain_newstack(void)
"orq $0x600, %rax;"
"movq %rax, %cr4;");
double wat2 = 13.37;
wat2 *= 6.66;
kprintf("%x\n", wat2);
u64 wat;
__asm__ volatile("movq %%xmm0, %0;" : "=r" (wat));
kprintf("%x\n", wat);
//lua_State *State = lua_newstate(l_alloc, NULL);
//luaL_checkversion(State);
//luaL_openlibs(State);
for (;;) {}
cpp_call_ctors();
cpp_start_ckernel();
kprintf("[i] Returned from Tier1, sleeping forever.\n");
LOOPFOREVER;
/*pic_init(0, 0);
ps2_init_simple();
@ -132,10 +131,5 @@ void kmain_newstack(void)
for (u32 Rl = 0; Rl < R; Rl++)
{
krand();
}
cpp_call_ctors();
cpp_start_ckernel();
kprintf("[i] Returned from Tier1, sleeping forever.\n");
LOOPFOREVER;*/
} */
}

View File

@ -1,11 +1,11 @@
#include "Tier1/CKernel.h"
#include "Tier1/CPageFaultDispatcher.h"
#include "Tier1/CPageDirectory.h"
#include "Tier1/CTask.h"
#include "Tier1/CScheduler.h"
//#include "Tier1/CPageFaultDispatcher.h"
//#include "Tier1/CPageDirectory.h"
//#include "Tier1/CTask.h"
//#include "Tier1/CScheduler.h"
#include "Tier1/Util/CVector.h"
#include "Tier1/Util/CLinearList.h"
#include "Tier1/CTimer.h"
//#include "Tier1/CTimer.h"
using namespace cb;
@ -44,7 +44,7 @@ void CKernel::Start(void)
m_Logger = new CLogger();
CTask *KernelTask = CreateKernelTask();
/*CTask *KernelTask = CreateKernelTask();
kprintf("[i] Kernel task has TID %i.\n", KernelTask->GetPID());
CScheduler::AddTask(KernelTask);
CScheduler::Enable();
@ -73,10 +73,10 @@ void CKernel::Start(void)
}
kprintf("[i] Hello! I'm the child process %i.\n", CTimer::GetTicks());
}
}
}*/
}
extern T_PAGING_DIRECTORY g_kernel_page_directory;
/*extern T_PAGING_DIRECTORY g_kernel_page_directory;
extern CPageDirectory *g_KernelPageDirectory;
CTask *CKernel::CreateKernelTask(void)
{
@ -101,5 +101,5 @@ CTask *CKernel::CreateKernelTask(void)
Task->m_KernelSize = 0x20000000;
return Task;
}
}*/

15
Kernel/src/Tier1/CKnot.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef __CKNOT_H__
#define __CKNOT_H__
// KNOT - Kinda Not an Object Tree, or more accurately, KNOT is Not an
// Object Tree.
namespace cb {
class CKnot {
public:
CKnot(void);
~CKnot(void);
};
};
#endif

View File

@ -0,0 +1,8 @@
align 8
section .text
global ctask_getrip
ctask_getrip:
pop rax
jmp rax

191
Kernel/src/Tier1/CTask.cpp Normal file
View File

@ -0,0 +1,191 @@
#include "Tier1/CTask.h"
using namespace cb;
extern "C" {
// #include "Tier0/physical_alloc.h"
#include "Tier0/paging.h"
#include "Tier0/panic.h"
#include "Tier0/kstdio.h"
};
//#include "Tier1/CScheduler.h"
//#include "Tier1/CTimer.h"
///extern CPageDirectory *g_KernelPageDirectory;
u32 CTaskNextPID = 0;
CTask::CTask(bool User, bool Empty)
{
m_User = User;
if (!Empty)
{
// CreateDirectory();
// CreateStack();
// CopyKernelMemory();
m_CreatedStack = true;
}
else
m_CreatedStack = false;
m_Status = ETS_RUNNING;
m_PID = CTaskNextPID++;
m_Priority = ETP_NORMAL;
m_Owner = 0;
m_Ring = (User ? ETR_RING3 : ETR_RING0);
}
CTask::~CTask(void)
{
// if (m_CreatedStack)
// physmem_free_page(m_StackStart / (4 * 1024));
}
// void CTask::CreateStack(void)
// {
// m_StackStart = physmem_allocate_page() * 4 * 1024;
// m_StackSize = TASK_MAP_STACK_SIZE;
// m_Directory->MapTable(TASK_MAP_STACK_START, m_StackStart, 1, 1);
// }
// void CTask::CreateDirectory(void)
// {
// m_Directory = new CPageDirectory();
// }
// void CTask::CopyKernelMemory(void)
// {
// //Copy all the kernel tables...
// m_KernelStart = 0xC0000000;
// m_KernelSize = 0x20000000;
// u32 NumTables = m_KernelSize / (4 * 1024 * 1024);
// for (u32 i = 0; i < NumTables; i++)
// m_Directory->LinkTable(m_KernelStart + i * 4 * 1024 * 1024,
// g_KernelPageDirectory);
// m_Directory->LinkTable(0x00000000, g_KernelPageDirectory);
// m_Directory->LinkTable(0x00400000, g_KernelPageDirectory);
// //m_Directory->LinkTable(0x00800000, g_KernelPageDirectory);
// //m_Directory->LinkTable(0x01000000, g_KernelPageDirectory);
// }
// void CTask::CopyStack(CTask *Source)
// {
// CPageDirectory *SourcePD = Source->GetPageDirectory();
// SourcePD->CopyPage(TASK_MAP_STACK_START, m_Directory, m_User, 1);
// }
// CPageDirectory *CTask::GetPageDirectory(void)
// {
// return m_Directory;
// }
// CTask *CTask::Fork(void)
// {
// __asm__ volatile("cli");
// volatile u32 ESP, EBP;
// CTask *ParentTask = CScheduler::GetCurrentTask();
// CTask *NewTask = new CTask(m_User);
// NewTask->m_Owner = m_Owner;
// NewTask->m_Ring = m_Ring;
// if (m_User)
// {
// //TODO: Write code for userland
// PANIC("Cannot fork usermode program!");
// }
// __asm__ volatile("mov %%esp, %0" : "=r"(ESP));
// __asm__ volatile("mov %%ebp, %0" : "=r"(EBP));
// NewTask->CopyStack(this);
// volatile u32 ForkPoint = ctask_geteip();
// if (CScheduler::GetCurrentTask() == ParentTask)
// {
// NewTask->m_ESP = ESP;
// NewTask->m_EBP = EBP;
// NewTask->m_EIP = ForkPoint;
// kprintf("[i] Forked: TID %i, ESP %x, EBP %x, EIP %x...\n", NewTask->m_PID,
// ESP, EBP, ForkPoint);
// CScheduler::AddTask(NewTask);
// __asm__ volatile("sti");
// return ParentTask;
// }
// else
// {
// __asm__ volatile("sti");
// //for(;;){}
// return NewTask;
// }
// }
// void CTask::Dump(void)
// {
// kprintf("d:%x s:%x b:%x i:%x\n",m_Directory->m_Directory->PhysicalAddress,
// m_ESP, m_EBP, m_EIP);
// }
// __attribute__((optimize("O0"))) void CTask::Yield(void)
// {
// //kprintf("Entering NextTask\n");
// CScheduler::NextTask();
// //kprintf("returned from NextTask\n");
// }
// void CTask::WaitForSemaphore(T_SEMAPHORE *Semaphore)
// {
// __asm__ volatile ("cli");
// m_Status = ETS_WAITING_FOR_SEMAPHORE;
// m_StatusData = m_Directory->Translate((u32)Semaphore);
// Yield();
// __asm__ volatile ("sti");
// }
// void CTask::WaitForSemaphore(CSemaphore *Semaphore)
// {
// __asm__ volatile ("cli");
// m_Status = ETS_WAITING_FOR_SEMAPHORE;
// m_StatusData = m_Directory->Translate((u32)Semaphore);
// Yield();
// __asm__ volatile ("sti");
// }
// void CTask::Disable(void)
// {
// __asm__ volatile ("cli");
// m_Status = ETS_DISABLED;
// Yield();
// __asm__ volatile ("sti");
// }
// void CTask::Enable(void)
// {
// __asm__ volatile ("cli");
// m_Status = ETS_RUNNING;
// __asm__ volatile ("sti");
// }
// void CTask::Sleep(u32 Ticks)
// {
// __asm__ volatile ("cli");
// m_Status = ETS_DISABLED;
// CTimer::Create(Ticks, 1, WakeUp, (u32)this);
// Yield();
// __asm__ volatile ("sti");
// }
// bool CTask::WakeUp(u32 Extra)
// {
// CTask *Task = (CTask*)Extra;
// Task->Enable();
// return true;
// }

View File

@ -1,8 +0,0 @@
align 4
section .text
global ctask_geteip
ctask_geteip:
pop eax
jmp eax

View File

@ -1,191 +0,0 @@
#include "Tier1/CTask.h"
using namespace cb;
extern "C" {
#include "Tier0/physical_alloc.h"
#include "Tier0/paging.h"
#include "Tier0/panic.h"
#include "Tier0/kstdio.h"
};
#include "Tier1/CScheduler.h"
#include "Tier1/CTimer.h"
extern CPageDirectory *g_KernelPageDirectory;
u32 CTaskNextPID = 0;
CTask::CTask(bool User, bool Empty)
{
m_User = User;
if (!Empty)
{
CreateDirectory();
CreateStack();
CopyKernelMemory();
m_CreatedStack = true;
}
else
m_CreatedStack = false;
m_Status = ETS_RUNNING;
m_PID = CTaskNextPID++;
m_Priority = ETP_NORMAL;
m_Owner = 0;
m_Ring = (User ? ETR_RING3 : ETR_RING0);
}
CTask::~CTask(void)
{
if (m_CreatedStack)
physmem_free_page(m_StackStart / (4 * 1024));
}
void CTask::CreateStack(void)
{
m_StackStart = physmem_allocate_page() * 4 * 1024;
m_StackSize = TASK_MAP_STACK_SIZE;
m_Directory->MapTable(TASK_MAP_STACK_START, m_StackStart, 1, 1);
}
void CTask::CreateDirectory(void)
{
m_Directory = new CPageDirectory();
}
void CTask::CopyKernelMemory(void)
{
//Copy all the kernel tables...
m_KernelStart = 0xC0000000;
m_KernelSize = 0x20000000;
u32 NumTables = m_KernelSize / (4 * 1024 * 1024);
for (u32 i = 0; i < NumTables; i++)
m_Directory->LinkTable(m_KernelStart + i * 4 * 1024 * 1024,
g_KernelPageDirectory);
m_Directory->LinkTable(0x00000000, g_KernelPageDirectory);
m_Directory->LinkTable(0x00400000, g_KernelPageDirectory);
//m_Directory->LinkTable(0x00800000, g_KernelPageDirectory);
//m_Directory->LinkTable(0x01000000, g_KernelPageDirectory);
}
void CTask::CopyStack(CTask *Source)
{
CPageDirectory *SourcePD = Source->GetPageDirectory();
SourcePD->CopyPage(TASK_MAP_STACK_START, m_Directory, m_User, 1);
}
CPageDirectory *CTask::GetPageDirectory(void)
{
return m_Directory;
}
CTask *CTask::Fork(void)
{
__asm__ volatile("cli");
volatile u32 ESP, EBP;
CTask *ParentTask = CScheduler::GetCurrentTask();
CTask *NewTask = new CTask(m_User);
NewTask->m_Owner = m_Owner;
NewTask->m_Ring = m_Ring;
if (m_User)
{
//TODO: Write code for userland
PANIC("Cannot fork usermode program!");
}
__asm__ volatile("mov %%esp, %0" : "=r"(ESP));
__asm__ volatile("mov %%ebp, %0" : "=r"(EBP));
NewTask->CopyStack(this);
volatile u32 ForkPoint = ctask_geteip();
if (CScheduler::GetCurrentTask() == ParentTask)
{
NewTask->m_ESP = ESP;
NewTask->m_EBP = EBP;
NewTask->m_EIP = ForkPoint;
kprintf("[i] Forked: TID %i, ESP %x, EBP %x, EIP %x...\n", NewTask->m_PID,
ESP, EBP, ForkPoint);
CScheduler::AddTask(NewTask);
__asm__ volatile("sti");
return ParentTask;
}
else
{
__asm__ volatile("sti");
//for(;;){}
return NewTask;
}
}
void CTask::Dump(void)
{
kprintf("d:%x s:%x b:%x i:%x\n",m_Directory->m_Directory->PhysicalAddress,
m_ESP, m_EBP, m_EIP);
}
__attribute__((optimize("O0"))) void CTask::Yield(void)
{
//kprintf("Entering NextTask\n");
CScheduler::NextTask();
//kprintf("returned from NextTask\n");
}
void CTask::WaitForSemaphore(T_SEMAPHORE *Semaphore)
{
__asm__ volatile ("cli");
m_Status = ETS_WAITING_FOR_SEMAPHORE;
m_StatusData = m_Directory->Translate((u32)Semaphore);
Yield();
__asm__ volatile ("sti");
}
void CTask::WaitForSemaphore(CSemaphore *Semaphore)
{
__asm__ volatile ("cli");
m_Status = ETS_WAITING_FOR_SEMAPHORE;
m_StatusData = m_Directory->Translate((u32)Semaphore);
Yield();
__asm__ volatile ("sti");
}
void CTask::Disable(void)
{
__asm__ volatile ("cli");
m_Status = ETS_DISABLED;
Yield();
__asm__ volatile ("sti");
}
void CTask::Enable(void)
{
__asm__ volatile ("cli");
m_Status = ETS_RUNNING;
__asm__ volatile ("sti");
}
void CTask::Sleep(u32 Ticks)
{
__asm__ volatile ("cli");
m_Status = ETS_DISABLED;
CTimer::Create(Ticks, 1, WakeUp, (u32)this);
Yield();
__asm__ volatile ("sti");
}
bool CTask::WakeUp(u32 Extra)
{
CTask *Task = (CTask*)Extra;
Task->Enable();
return true;
}

View File

@ -13,8 +13,15 @@ SECTIONS
{
_code = .;
*(.text)
*(.text.startup)
*(.rodata*)
. = ALIGN(8);
_start_ctors = .;
*(.ctors)
_end_ctors = .;
. = ALIGN(4096);
}
@ -25,13 +32,6 @@ SECTIONS
. = ALIGN(4096);
}
.ehframe : AT(ADDR(.ehframe) - KERNEL_VMA)
{
_ehframe = .;
*(.ehframe)
. = ALIGN(4096);
}
.bss : AT(ADDR(.bss) - KERNEL_VMA)
{
_bss = .;
@ -46,5 +46,6 @@ SECTIONS
{
*(.comment)
*(.debug*)
*(.eh_frame)
}
}