Merge branch 'master' of http://code.hackerspace.pl/q3k/Cucumber into alentours-dev

alentours-dev
q3k 2012-10-28 10:26:31 +01:00
commit 247dcd80dc
11 changed files with 390 additions and 58 deletions

1
.gitignore vendored
View File

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

View File

@ -22,7 +22,7 @@ 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
@ -76,7 +76,7 @@ Lua: $(LUA)
kernel.bin: version-gen Tier0 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..."

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

@ -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

@ -19,12 +19,33 @@
#include "Tier0/exceptions.h"
#include "Tier0/panic.h"
//#include "Tier0/prng.h"
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "lualib.h"
extern u64 _start;
extern u64 _end;
void kmain_newstack(void);
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
if (nsize == 0)
{
kfree(ptr);
return NULL;
}
if (ptr != 0)
{
void *newptr = kmalloc(nsize);
kmemcpy(newptr, ptr, nsize);
return newptr;
}
return kmalloc(nsize);;
}
// Real kernel entry point, called from loader
void kmain(u32 LoadContextAddress)
{
@ -81,6 +102,36 @@ void kmain(u32 LoadContextAddress)
kmain_newstack_ptr();
}
static int traceback (lua_State *L) {
const char *msg = lua_tostring(L, 1);
if (msg)
{
kprintf("Lua traceback: %s\n", msg);
return 0;
}
return 1;
}
int doluastring(lua_State *State, s8 *Code)
{
kprintf("[i] Running Lua string:\n %s\n", Code);
int Buffer = luaL_loadbuffer(State, Code, kstrlen(Code), "kmain-dostring");
if (Buffer != LUA_OK)
{
kprintf("[e] doluastring: Could not load Lua buffer!\n");
return 1;
}
int Base = lua_gettop(State);
lua_pushcfunction(State, traceback);
lua_insert(State, Base);
lua_pcall(State, 0, 0, 1);
lua_remove(State, Base);
return 0;
}
void kmain_newstack(void)
{
@ -96,7 +147,7 @@ void kmain_newstack(void)
exceptions_init_simple();
apic_enable_lapic();
heap_init_simple();
// enable FPU/SSE...
__asm__ volatile(
"movq %cr0, %rax;"
@ -107,14 +158,10 @@ 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);
doluastring(State, "print(table.concat({'Lua', 'is', 'awesome!'}, ' '))");
for (;;) {}
/*pic_init(0, 0);