Merge pull request #4 from os-k-team/kaleid

Kaleid
This commit is contained in:
Julian B 2019-01-02 15:30:33 +00:00 committed by GitHub
commit d6602b9175
52 changed files with 2086 additions and 616 deletions

View File

@ -7,89 +7,21 @@
# Desc: Project Makefile # # Desc: Project Makefile #
#----------------------------------------------------------------------------# #----------------------------------------------------------------------------#
CCNAME="/opt/cross-cc/bin/x86_64-elf-gcc" kernel:
CC2NAME=gcc # compiler for testing cpp ./Makefile.in > Makefile.out
CLDSCR=-T kernel.ld python ./idttool.py
CWARNS= -pedantic -Wall -Wextra -Werror make kernel -f Makefile.out.2
CFLAGS=-nostdlib -ffreestanding -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 rm Makefile.out Makefile.out.2
CINCLUDES=-I./kaleid
CDEFINES=
CC=$(CCNAME) $(CWARNS) $(CFLAGS) $(CDEFINES) $(CINCLUDES) kernel-asm:
KCC=$(CC) -D_KALEID_KERNEL cpp -D_TO_ASM ./Makefile.in > Makefile.out
python ./idttool.py
make kernel -f Makefile.out.2
rm Makefile.out Makefile.out.2
ASM=nasm tests:
ASMFLAGS= cpp -D_TESTS ./Makefile.in > Makefile.out
BOOTFLAGS=-f bin python ./idttool.py
make tests -f Makefile.out.2
BINDIR=../bin rm Makefile.out Makefile.out.2
OBJDIR=../obj
BOOTDIR=boot
COMMDIR=kaleid/common
KERNDIR=kaleid/kernel
SYSTDIR=kaleid/system
LINXDIR=kaleid/linux
all: bootloader
boot.mbr.s: $(BOOTDIR)/mbr.s $(BOOTDIR)/mbr.inc
$(ASM) $(BOOTFLAGS) $(BOOTDIR)/mbr.s -o $(OBJDIR)/boot/mbr.bin
boot.loader.s: $(BOOTDIR)/loader.s
$(ASM) $(BOOTFLAGS) $(BOOTDIR)/loader.s -o $(OBJDIR)/boot/loader.bin
bootloader: boot.mbr.s boot.loader.s
cp $(OBJDIR)/boot/mbr.bin $(BINDIR)/mbr.bin
cp $(OBJDIR)/boot/loader.bin $(BINDIR)/loader.bin
#----------------------------------------------------------------------------#
# TESTING MAKEFILE
pseudo_kern:
$(ASM) $(BOOTFLAGS) $(BOOTDIR)/pseudo_kernel.s -o $(OBJDIR)/boot/pkernel.bin
testing: bootloader pseudo_kern
cat $(BINDIR)/bootloader.bin $(OBJDIR)/boot/pkernel.bin > $(BINDIR)/boot.bin
#----------------------------------------------------------------------------#
# COMMON MAKEFILE
COBJDIR=$(OBJDIR)/$(COMMDIR)
LOBJDIR=$(OBJDIR)/$(LINXDIR)
COMMDEPS=$(COMMDIR)/common.h $(COMMDIR)/assert.h $(COMMDIR)/atomic.h $(COMMDIR)/config.h \
$(COMMDIR)/status.h
COMMOBJS=$(COBJDIR)/lib/string.o $(COBJDIR)/lib/status.o
common: $(COMMDEPS) $(COMMDIR)/lib/string.c $(COMMDIR)/lib/status.c
$(KCC) -c $(COMMDIR)/lib/string.c -o $(COBJDIR)/lib/string.o
$(KCC) -c $(COMMDIR)/lib/status.c -o $(COBJDIR)/lib/status.o
CCC=$(CC2NAME) $(CWARNS) $(CDEFINES) $(CINCLUDES)
common-test:
$(CCC) -c $(COMMDIR)/lib/string.c -o $(COBJDIR)/lib/string.o
$(CCC) -c $(COMMDIR)/lib/status.c -o $(COBJDIR)/lib/status.o
$(CCC) -c $(LINXDIR)/test-common.c -o $(LOBJDIR)/test-common.o
$(CCC) $(COMMOBJS) $(LOBJDIR)/test-common.o -o $(BINDIR)/kaleid-common.elf
#----------------------------------------------------------------------------#
# KERNEL MAKEFILE
KOBJDIR=$(OBJDIR)/$(KERNDIR)
KERNDEPS=common $(KERNDIR)/init.h $(KERNDIR)/io/terminal.h $(KERNDIR)/io/ports.h $(KERNDIR)/ke/panic.h
KERNSRCS=$(KERNDIR)/init.c $(KERNDIR)/ke/panic.c $(KERNDIR)/io/ports.c $(KERNDIR)/io/terminal.c
KERNOBJS=$(KOBJDIR)/init.o $(KOBJDIR)/ke/panic.o $(KOBJDIR)/io/ports.o $(KOBJDIR)/io/terminal.o
kernel: common $(KERNSRCS)
$(KCC) -c $(KERNDIR)/init.c -o $(KOBJDIR)/init.o
$(KCC) -c $(KERNDIR)/ke/panic.c -o $(KOBJDIR)/ke/panic.o
$(KCC) -c $(KERNDIR)/io/ports.c -o $(KOBJDIR)/io/ports.o
$(KCC) -c $(KERNDIR)/io/terminal.c -o $(KOBJDIR)/io/terminal.o
$(KCC) $(CLDSCR) $(COMMOBJS) $(KERNOBJS) -o $(BINDIR)/kaleid-kernel.elf
#----------------------------------------------------------------------------#

107
src/Makefile.in Normal file
View File

@ -0,0 +1,107 @@
//----------------------------------------------------------------------------#
// GNU GPL OS/K #
// #
// Authors: spectral` #
// NeoX #
// #
// Desc: Project Makefile #
//----------------------------------------------------------------------------#
// The madman's Makefile
#include "preproc.h"
CCNAME="/opt/cross-cc/bin/x86_64-elf-gcc"
CC2NAME=gcc
COPTIM=-O2
CLDSCR=-T kernel.ld
CWARNS=-pedantic -Wall -Wextra -Werror
CINCLUDES=-isystem./kaleid/include
CDEFINES=
CFLAGS1=-nostdlib -ffreestanding -mcmodel=large
CFLAGS2=-mno-red-zone -mno-mmx -mno-sse -mno-sse2
CFLAGS=$(CFLAGS1) $(CFLAGS2) $(SFLAG)
CC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CDEFINES) $(CINCLUDES)
ASM=nasm
ASMFLAGS=
BOOTFLAGS=-f bin
BINDIR=../bin
OBJDIR=../obj
BOOTDIR=boot
COMMDIR=kaleid/common
KERNDIR=kaleid/kernel
SYSTDIR=kaleid/system
LINXDIR=kaleid/common/test
INCDIR=kaleid/include
all: bootloader kernel
boot.mbr.s: $(BOOTDIR)/mbr.s $(BOOTDIR)/mbr.inc
$(ASM) $(BOOTFLAGS) $(BOOTDIR)/mbr.s -o $(OBJDIR)/boot/mbr.bin
boot.loader.s: $(BOOTDIR)/loader.s
$(ASM) $(BOOTFLAGS) $(BOOTDIR)/loader.s -o $(OBJDIR)/boot/loader.bin
bootloader: boot.mbr.s boot.loader.s
cp $(OBJDIR)/boot/mbr.bin $(BINDIR)/mbr.bin
cp $(OBJDIR)/boot/loader.bin $(BINDIR)/loader.bin
//----------------------------------------------------------------------------#
// TESTING MAKEFILE
pseudo_kern:
$(ASM) $(BOOTFLAGS) $(BOOTDIR)/pseudo_kernel.s -o $(OBJDIR)/boot/pkernel.bin
testing: bootloader pseudo_kern
cat $(BINDIR)/bootloader.bin $(OBJDIR)/boot/pkernel.bin > $(BINDIR)/boot.bin
//----------------------------------------------------------------------------#
// COMMON MAKEFILE
COBJDIR=$(OBJDIR)/$(COMMDIR)
LOBJDIR=$(OBJDIR)/$(LINXDIR)
COMMOBJS=COBJ5(string,status,rand,memory,arith) COBJ4(itoa,ltoa,utoa,ultoa)
TCC=$(CC2NAME) $(COPTIM) $(CWARNS) $(CINCLUDES)
KCC=$(CC) -D_OSK_SOURCE -D_KALEID_KERNEL
comm-convert:
COMPILE_CONVRT(itoa) -D_NEED_ITOA
COMPILE_CONVRT(ltoa) -D_NEED_LTOA
COMPILE_CONVRT(utoa) -D_NEED_UTOA
COMPILE_CONVRT(ultoa) -D_NEED_ULTOA
common: comm-convert
COMPILE_COMMON(rand)
COMPILE_COMMON(arith)
COMPILE_COMMON(string)
COMPILE_COMMON(status)
COMPILE_COMMON(memory)
tests: common
$(TCC) -c $(LINXDIR)/test-common.c -o $(LOBJDIR)/test-common.o
$(TCC) $(COMMOBJS) $(LOBJDIR)/test-common.o -o $(BINDIR)/kaleid-common.elf
//----------------------------------------------------------------------------#
// KERNEL MAKEFILE
KOBJDIR=$(OBJDIR)/$(KERNDIR)
KERNOBJS=KOBJ5(init,ke/lock,ke/panic,ke/state,io/ports) KOBJ1(io/terminal)
kernel: common
COMPILE_KERNEL(init)
COMPILE_KERNEL(ke/lock)
COMPILE_KERNEL(ke/state)
COMPILE_KERNEL(ke/panic)
COMPILE_KERNEL(io/ports)
COMPILE_KERNEL(io/terminal)
LINK_KERNEL(kaleid-kernel.elf)
//----------------------------------------------------------------------------#

View File

@ -1,11 +1,11 @@
//----------------------------------------------------------------------------// ---------------------------------------------------------------------
// GNU GPL OS/K // GNU GPL OS/K
// //
// Authors: spectral` // Authors: spectral`
// NeoX // NeoX
// //
// Desc: Folder description - "boot" // Desc: Folder description - "boot"
//----------------------------------------------------------------------------// ---------------------------------------------------------------------
This folder contains the source for OS/K's bootloader. This folder contains the source for OS/K's bootloader.

14
src/idttool.py Normal file
View File

@ -0,0 +1,14 @@
# don't mind this file
f1 = open("Makefile.out", "r+")
f2 = open("Makefile.out.2", "w+")
fl = f1.readlines()
for ln in fl:
if ln[0] == ' ' and ln[1] != ' ':
f2.write('\t')
f2.write(ln)
f1.close()
f2.close()

53
src/kaleid/common/arith.c Normal file
View File

@ -0,0 +1,53 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Arithmetical functions //
//----------------------------------------------------------------------------//
// do not mask anything
#define _KALMASK_H
#include <kaleid.h>
int _osk_abs(int x)
{
return abs(x);
}
long _osk_labs(long x)
{
return labs(x);
}
int _osk_min(int x, int y)
{
return min(x, y);
}
long _osk_lmin(long x, long y)
{
return lmin(x, y);
}
int _osk_max(int x, int y)
{
return max(x, y);
}
long _osk_lmax(long x, long y)
{
return lmax(x, y);
}
div_t _osk_div(int x, int y)
{
return div(x, y);
}
ldiv_t _osk_ldiv(long x, long y)
{
return ldiv(x, y);
}

View File

@ -1,51 +0,0 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Assertions //
//----------------------------------------------------------------------------//
#ifndef _KALCOMM_ASSERT_H
#define _KALCOMM_ASSERT_H
#ifndef _KALCOMM_COMMON_H
#error "don't include common/types.h without common/common.h"
#endif
#ifdef _OSK_SOURCE
#if !defined(_NO_DEBUG) && !defined(NDEBUG)
// uses panic() in kernel, abort() in system
noreturn void ___assert_handler(const char *, const char *, int, const char *);
#define assert(x) do{if(unlikely(!(x)))___assert_handler(#x, __FILE__, __LINE__, __func__);}while(0);
#else // not debugging
#if !defined(NDEBUG)
#define NDEBUG 1
#endif
#if !defined(_NO_DEBUG)
#define _NO_DEBUG 1
#endif
#define assert(x)
#endif
#else // !defined(_OSK_SOURCE)
#if defined(_NO_DEBUG) && !defined(NDEBUG)
#define NDEBUG 1
#endif
#include <assert.h>
#endif
#endif

View File

@ -1,51 +0,0 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Standard include file for both kernel/ and system/ //
//----------------------------------------------------------------------------//
#ifndef _KALCOMM_COMMON_H
#define _KALCOMM_COMMON_H
#if !defined(_OSK_SOURCE) && (defined(_KALEID_KERNEL) || defined(_KALEID_SYSTEM))
#define _OSK_SOURCE 1
#endif
#if !defined(TRUE) && !defined(FALSE)
# define TRUE 1
# define FALSE 0
#endif
#ifdef _OSK_SOURCE
# define YES 1
# define NO 0
#endif
#ifndef NULL
# define NULL ((void*)0)
#endif
#ifndef PACKED
# define PACKED __attribute__((packed))
#endif
#ifndef noreturn
# define noreturn __attribute__((noreturn))
#endif
#if !defined(likely) && !defined(unlikely)
# define likely(x) __builtin_expect((x), 1)
# define unlikely(x) __builtin_expect((x), 0)
#endif
#include "common/types.h"
#include "common/config.h"
#include "common/atomic.h"
#include "common/status.h"
#include "common/assert.h"
#endif

View File

@ -7,43 +7,60 @@
// Desc: Conversion utilities // // Desc: Conversion utilities //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include "common/convert.h" #include <kaleid.h>
// //
// Digits table for bases <=36 // Digits table for bases <=36
// //
static const char digits[36] = static const char digits[36] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// //
// Integer to string in any base between 2 and 36 (included) // Integer to string in any base between 2 and 36 (included)
// //
#if defined(_NEED_ITOA)
char *itoa(int i, char *str, int base) char *itoa(int i, char *str, int base)
#elif defined(_NEED_LTOA)
char *ltoa(long i, char *str, int base)
#elif defined(_NEED_UTOA)
char *utoa(uint i, char *str, int base)
#elif defined(_NEED_ULTOA)
char *ultoa(ulong i, char *str, int base)
#else
#error "What am I supposed to declare?"
#endif
{ {
#if defined(_NEED_ITOA) || defined(_NEED_LTOA)
int neg = 0; int neg = 0;
#endif
char *orig = str; char *orig = str;
if (base < 2 || base > 36) if (base < 2 || base > 36)
return NULL; return NULL;
#if defined(_NEED_ITOA) || defined(_NEED_LTOA)
// deal with negatives // deal with negatives
if (i < 0) { if (i < 0) {
neg = 1; neg = 1;
i = -i; i = -i;
} }
#endif
// deal with zero separatly // deal with zero separately
if (i == 0) { if (i == 0) {
*str++ = '0'; *str++ = '0';
} }
// compute digits... in reverse order (XXX?) // compute digits... in reverse order
while (i > 0) { while (i > 0) {
*str++ = digits[i % base]; *str++ = digits[i % base];
i /= base; i /= base;
} }
#if defined(_NEED_ITOA) || defined(_NEED_LTOA)
if (neg) *str++ = '-'; if (neg) *str++ = '-';
#endif
*str = '\0'; *str = '\0';
return reverse(orig); return reverse(orig);

View File

@ -1 +0,0 @@

194
src/kaleid/common/memory.c Normal file
View File

@ -0,0 +1,194 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: mem*() functions //
//----------------------------------------------------------------------------//
#include <kaleid.h>
#include <kallims.h>
//------------------------------------------//
// memset() family //
//------------------------------------------//
//
// Set "bytes"-many bytes starting from ptr to val
//
void *memsetb(void *ptr, int val, size_t bytes)
{
uchar *uptr = (uchar *)ptr;
// deal with bytes before start of the first aligned qword
while (((ulong)uptr % QWORD_ALIGN) > 0 && bytes--) {
*uptr++ = (uchar)val;
}
// we're qword-aligned now
if (bytes > QWORD_SIZE) {
const ulong uval = ((ulong)val << 56) | ((ulong)val << 48)
| ((ulong)val << 40) | ((ulong)val << 32)
| ((ulong)val << 24) | ((ulong)val << 16)
| ((ulong)val << 8) | ((ulong)val);
ulong *uqptr = (ulong *)ptr;
// move qword by qword
while (bytes > QWORD_SIZE) {
*uqptr++ = uval;
bytes -= QWORD_SIZE;
}
uptr = (uchar *)(ulong)uqptr;
}
// deal with what's left
while (bytes--) {
*uptr++ = (uchar)val;
}
return ptr;
}
//
// Set "words"-many words starting from ptr to val
//
void *memsetw(void *ptr, int val, size_t words)
{
ushort *uptr = (ushort *)ptr;
// can't we do this an aligned way?
if unlikely (((ulong)uptr % WORD_ALIGN) > 0) {
// no, we can't align ourselves
while (words--) {
// do it the hard way
*uptr++ = (ushort)val;
}
// too bad '-'
return uptr;
}
// deal with words before start of the first aligned qword
while (((ulong)uptr % QWORD_ALIGN) > 0 && words--) {
*uptr++ = (ushort)val;
}
// we're aligned for sure
if (words > QWORDS_TO_WORDS(1)) {
const ulong uval = ((ulong)val << 48) | ((ulong)val << 32)
| ((ulong)val << 16) | ((ulong)val);
ulong *uqptr = (ulong *)uptr;
// move qword by qword
while (words > QWORDS_TO_WORDS(1)) {
words -= QWORDS_TO_WORDS(1);
*uqptr++ = uval;
}
uptr = (ushort *)(ulong)uqptr;
}
// deal with what's left
while (words--) {
*uptr++ = (ushort)val;
}
return ptr;
}
//
// Set "dwords"-many dwords starting from ptr to val
// XXX unimplemented
//
void *memsetd(void *ptr, int val, size_t dwords)
{
(void)val;
(void)dwords;
return ptr;
}
//
// Set "qwords"-many qwords starting from ptr to val
//
void *memsetq(void *ptr, long val, size_t qwords)
{
ulong *uptr = (ulong *)ptr;
while (qwords--) *uptr++ = (ulong)val;
return ptr;
}
//
// Set "bytes"-many bytes starting from ptr to 0
//
// WARNING
// Assume "bytes" is large, for small sizes
// use memset(ptr, 0, bytes) directly
//
void *memzero(void *ptr, size_t bytes)
{
return memsetb(ptr, 0, bytes);
}
//
// Copy "bytes"-many bytes of src to dst
// Does not deal with overlapping blocks (memmove's job)
//
void *memcpy(void *dst, const void *src, size_t bytes)
{
const ulong *usrc = (const ulong *)src;
ulong *udst = (ulong *)dst;
if unlikely (bytes == 0) return dst;
// can we align them both at once?
if unlikely ((ulong)src % WORD_ALIGN == 1
&& (ulong)dst % WORD_ALIGN == 1) {
const uchar *ubsrc = (const uchar *)usrc;
uchar *ubdst = (uchar *)udst;
*ubdst++ = *ubsrc++;
bytes--;
udst = (ulong *)ubdst;
usrc = (ulong *)ubsrc;
}
const ushort *uwsrc = (const ushort *)usrc;
ushort *uwdst = (ushort *)udst;
// align either dst or src for qword access
while ((ulong)dst % QWORD_ALIGN > 0
&& (ulong)src % QWORD_ALIGN > 0
&& bytes > WORD_SIZE) {
*uwdst++ = *uwsrc++;
bytes -= WORD_SIZE;
}
udst = (ulong *)uwdst;
usrc = (ulong *)uwsrc;
// should be most of the job
while (bytes > QWORD_SIZE) {
*udst++ = *usrc++;
bytes -= QWORD_SIZE;
}
const uchar *ubsrc = (const uchar *)usrc;
ushort *ubdst = (ushort *)udst;
// deal with what's left
while (bytes--) {
*ubdst ++ = *ubsrc++;
}
return dst;
}

View File

@ -0,0 +1,179 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Standard include file for both kernel/ and system/ //
//----------------------------------------------------------------------------//
#ifndef _KALCOMM_COMMON_H
#define _KALCOMM_COMMON_H
//------------------------------------------//
// PREPROCESSOR CONSTANTS //
//------------------------------------------//
#if !defined(_OSK_SOURCE)
# if defined(_KALEID_KERNEL) || defined(_KALEID_SYSTEM)
# define _OSK_SOURCE 1
# endif
#endif
#if !defined(TRUE) && !defined(FALSE)
# define TRUE (1)
# define FALSE (0)
#endif
#ifdef _OSK_SOURCE
# define YES (1)
# define NO (0)
#endif
#ifndef NULL
# define NULL ((void *)0)
#endif
#ifndef PACKED
# define PACKED __attribute__((packed))
#endif
#ifndef noreturn
# define noreturn __attribute__((noreturn))
#endif
#if !defined(likely) && !defined(unlikely)
# define likely(x) (__builtin_expect((x), 1))
# define unlikely(x) (__builtin_expect((x), 0))
#endif
#ifndef alignof
# define alignof _Alignof
#endif
#ifndef INITOK
# define INITOK ((unsigned int)0xCAFEBABE)
#endif
#ifndef KALAPI
# define KALAPI
#endif
#ifdef _KALEID_KERNEL
# include <kaleid/kernel/config.h>
#endif
//------------------------------------------//
// COMMON TYPES //
//------------------------------------------//
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef long long llong;
typedef unsigned long long ullong;
typedef long double ldouble;
typedef short port_t;
typedef short status_t;
#ifndef KEEP_KALCOMM_TYPES_MINIMAL
typedef _Bool bool;
typedef uint wchar_t;
typedef ullong size_t;
typedef llong ssize_t;
typedef size_t off_t;
typedef int atomic_t;
typedef ulong pid_t;
typedef void *va_list;
#endif
// XXX limits
//------------------------------------------//
// VALUES FOR "status_t" //
//------------------------------------------//
#ifndef _OSK_SOURCE
# define describe_status _osk_describe_status
#endif
// see in common/lib/status.c for status messages
const char *describe_status(status_t);
#define STATUS_FAILED(x) ((x) < 0))
#define STATUS_SUCCESS(x) (!STATUS_FAILED(x))
#define SUCCESS (0) // everything went fine
#define FAILED (-1)
#define ERROR FAILED // something went wrong
#define NOT_PERMITTED (-2)
#define ACCESS_DENIED (-3)
#define BAD_ARGUMENT (-4) // invalid arguments
#define BAD_ARG_RANGE (-5) // arguments out of range
#define BAD_ARG_NULL (-6) // unexpected NULL argument
#define TRY_AGAIN (-7) // EAGAIN
//------------------------------------------//
// INLINE ASM MACROS //
//------------------------------------------//
#ifdef _KALEID_KERNEL
# define DisableInterrupts() asm volatile ("cli")
# define EnableInterrupts() asm volatile ("sti")
# define HaltCPU() asm volatile ("hlt")
#endif
//------------------------------------------//
// assert()/DosAssert() SUPPORT //
//------------------------------------------//
#ifdef _OSK_SOURCE
#if !defined(_NO_DEBUG) && !defined(NDEBUG)
// uses panic() in kernel, abort() in system
noreturn void _assert_handler(const char *, const char *, int, const char *);
#define DosAssert(x) \
do { \
if unlikely(!(x)) \
_assert_handler(#x, __FILE__, __LINE__, __func__); \
} while (FALSE);
#define assert DosAssert
#else // not debugging
#if !defined(NDEBUG)
# define NDEBUG 1
#endif
#if !defined(_NO_DEBUG)
# define _NO_DEBUG 1
#endif
#define assert(x)
#endif
#else // !defined(_OSK_SOURCE)
#if defined(_NO_DEBUG) && !defined(NDEBUG)
# define NDEBUG 1
#endif
#include <assert.h>
#endif
//------------------------------------------//
// END OF "kaleid/common/common.h" //
//------------------------------------------//
#endif

View File

@ -0,0 +1,37 @@
---------------------------------------------------------------------
GNU GPL OS/K
Authors: spectral`
NeoX
Desc: Folder description - "kaleid/common"
---------------------------------------------------------------------
This is the folder containing the sources for Kaleid's C runtime library, linked
both to the kernel and to the system processes that run outiside the kernel.
It can also be compiled for Linux as a very basic C library, for test purposes.
This folder contains the following files:
- common.h
This file is to be included by every source file using the library.
It:
- Provides the elementary types (e.g. size_t) used through Kaleid.
- Defines the different values of the "status_t" type used
throughout Kaleid and related utilitary functions.
- Defines the macro "assert()". Currently any program wanting to
use this macro has to implement its own handler:
noreturn void _assert_handler(const char *cond,
const char *file,
int line,
const char *func)
but an overridable default handler will be furnished later.
- stlib.h
The include file for most of functions of the common library, notably
- Memory management utilities (e.g. memset())
- String manipulation utilities (e.g. strcpy())
- Type conversion utilities (e.g. itoa())

View File

@ -0,0 +1,90 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Kaleid C runtime library //
//----------------------------------------------------------------------------//
#ifndef _KALCOMM_STDLIB_H
#define _KALCOMM_STDLIB_H
//------------------------------------------//
// Typedefs //
//------------------------------------------//
#ifndef __size_t
#define __size_t
typedef unsigned long size_t;
#endif
#ifndef __va_list
#define __va_list
typedef __builtin_va_start va_list;
#endif
//------------------------------------------//
// Memory management utilitaries //
//------------------------------------------//
#ifndef _OSK_SOURCE
# define memcpy _osk_memcpy
# define memcmp _osk_memcmp
# define memzero _osk_memzero
#endif
#ifndef memset
# define memset memsetb
#endif
void *memsetb(void *, int, size_t);
void *memsetw(void *, int, size_t);
void *memsetd(void *, int, size_t);
void *memsetq(void *, long, size_t);
void *memzero(void *, size_t);
//------------------------------------------//
// String manipulation utilitaries //
//------------------------------------------//
#ifndef _OSK_SOURCE
# define strlen _osk_strlen
# define strcpy _osk_strcpy
# define strncpy _osk_strncpy
# define strrev _osk_strrev
# define reverse _osk_reverse
# define sprintf _osk_sprintf
# define snprintf _osk_snprintf
# define vsprintf _osk_vsprintf
# define vsnprintf _osk_vsnprintf
#endif
size_t strlen(const char *);
char *strcpy(char *, const char *);
char *strncpy(char *, const char *, size_t);
char *strrev(char *dest, const char *src);
char *reverse(char *);
int sprintf(char *, const char *, ...);
int snprintf(char *, size_t, const char *, ...);
int vsprintf(char *, const char *, va_list);
int vsnprintf(char *, size_t, const char *, va_list);
//------------------------------------------//
// Type conversion utilities //
//------------------------------------------//
#ifndef _OSK_SOURCE
# define itoa _osk_itoa
# define atoi _osk_atoi
#endif
char *itoa(int, char *, int);
//------------------------------------------//
// End of header file //
//------------------------------------------//
#endif

View File

@ -4,21 +4,32 @@
// Authors: spectral` // // Authors: spectral` //
// NeoX // // NeoX //
// // // //
// Desc: Conversion utilities // // Desc: RNG related functions //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#ifndef _KALCOMM_CONVERT_H #include <kaleid.h>
#define _KALCOMM_CONVERT_H #include <kallims.h>
#ifndef _KALCOMM_COMMON_H //
#include "common/common.h" // Seed value
#endif //
static ulong next = 7756;
#ifndef _OSK_SOURCE //
# define itoa _osk_itoa // Returns a pseudo-random integer
# define atoi _osk_atoi // To be improved
#endif //
int rand(void)
{
next = next * 1103515245 + 12345;
return (uint)(next / 65536) % INT_MAX;
}
char *itoa(int, char *, int); //
// (Re)Set the random seed
//
void srand(uint seed)
{
next = (ulong)seed;
}
#endif

View File

@ -7,7 +7,7 @@
// Desc: sprintf()-related functions // // Desc: sprintf()-related functions //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include "common/string.h" #include <kaleid.h>
// //
// Format str according to fmt using ellipsed arguments // Format str according to fmt using ellipsed arguments
@ -24,17 +24,14 @@ int sprintf(char *str, const char *fmt, ...)
return ret; return ret;
} }
//
// Format str according to fmt, using the va_list ap
//
int vsprintf(char *str, const char *fmt, va_list ap) int vsprintf(char *str, const char *fmt, va_list ap)
{ {
return vsnprintf(str, SIZE_T_MAX, fmt, ap); return vsnprintf(str, SIZE_T_MAX, fmt, ap);
} }
// //
// sprintf() but with a size limit: no more than n bytes are written in str // (v)sprintf() but with a size limit: no more than n bytes are written in str
// XXX null termination behavior? // XXX null termination behavior?
// //
int snprintf(char *str, size_t n, const char *fmt, ...) int snprintf(char *str, size_t n, const char *fmt, ...)
{ {
@ -48,9 +45,6 @@ int snprintf(char *str, size_t n, const char *fmt, ...)
return ret; return ret;
} }
//
// snprintf() but arguments
//
int vsnprintf(char *str, size_t n, const char *fmt, va_list ap) int vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
{ {
int ret = 0; int ret = 0;

View File

@ -7,7 +7,7 @@
// Desc: Implementation of describe_status() // // Desc: Implementation of describe_status() //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include "common/common.h" #include <kaleid.h>
static const char *descriptions[] = { static const char *descriptions[] = {
[-SUCCESS] = "Success", [-SUCCESS] = "Success",

View File

@ -1,41 +0,0 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Values for status_t //
//----------------------------------------------------------------------------//
#ifndef _KALCOMM_STATUS_H
#define _KALCOMM_STATUS_H
#ifndef _KALCOMM_COMMON_H
#error "don't include common/types.h without common/common.h"
#endif
#ifndef _OSK_SOURCE
# define describe_status _osk_describe_status
#endif
// see in common/lib/status.c for status messages
const char *describe_status(status_t);
#define STATUS_FAILED(x) ((x) < 0))
#define STATUS_SUCCESS(x) (!STATUS_FAILED(x))
#define SUCCESS (0) // everything went fine
#define FAILED (-1)
#define ERROR FAILED // something went wrong, can't be more precise
#define NOT_PERMITTED (-2)
#define ACCESS_DENIED (-3)
#define BAD_ARGUMENT (-4) // invalid arguments, can't be more precise
#define BAD_ARG_RANGE (-5) // arguments out of range
#define BAD_ARG_NULL (-6) // unexpected NULL argument
#define TRY_AGAIN (-7) // EAGAIN
#endif

View File

@ -7,9 +7,7 @@
// Desc: String-related functions // // Desc: String-related functions //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include "common/string.h" #include <kaleid.h>
// TODO multibyte, assembly
// //
// Returns str's length // Returns str's length
@ -34,22 +32,57 @@ char *strcpy(char *dest, const char *src)
} }
// //
// strcpy() but safer // strcpy() but always writes n bytes
// //
char *strncpy(char *dest, const char *src, size_t n) char *strncpy(char *dest, const char *src, size_t n)
{ {
size_t i; size_t it;
for (i = 0; i < n && src[i]; i++) { for (it = 0; it < n && src[it]; it++) {
dest[i] = src[i]; dest[it] = src[it];
} }
while (i < n) dest[i++] = 0; while (it < n) dest[it++] = 0;
return dest; return dest;
} }
// //
// Reverses a string // strncpy() but safer - always null-terminate,
// returns boolean indicating whether copy was complete
// XXX find a better name
// XXX actually implement this
//
size_t xstrcnpy(char *dest, const char *src, size_t n)
{
size_t it;
for (it = 0; it < n && src[it]; it++) {
dest[it] = src[it];
}
while (it < n) dest[it++] = 0;
return TRUE;
}
//
// Reverses the string src, putting the result into dest
//
char *strrev(char *dest, const char *src)
{
char *orig = dest;
size_t n = strlen(src);
dest[n--] = '\0';
while ((*dest++ = src[n--]));
return orig;
}
//
// Reverses a string, modifying it
// Returns a point to said string
// //
char *reverse(char *str) char *reverse(char *str)
{ {

View File

@ -1,43 +0,0 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Kaleid string library //
//----------------------------------------------------------------------------//
#ifndef _KALCOMM_STRING_H
#define _KALCOMM_STRING_H
#ifndef _KALCOMM_COMMON_H
#include "common/common.h"
#endif
#ifndef _OSK_SOURCE
# define strlen _osk_strlen
# define strcpy _osk_strcpy
# define strncpy _osk_strncpy
# define reverse _osk_reverse
# define sprintf _osk_sprintf
# define snprintf _osk_snprintf
# define vsprintf _osk_vsprintf
# define vsnprintf _osk_vsnprintf
#endif
size_t strlen(const char *);
char *strcpy(char *, const char *);
char *strncpy(char *, const char *, size_t);
char *reverse(char *);
int sprintf(char *, const char *, ...);
int snprintf(char *, size_t, const char *, ...);
int vsprintf(char *, const char *, va_list);
int vsnprintf(char *, size_t, const char *, va_list);
#endif

View File

@ -0,0 +1,83 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Test file for common/ //
//----------------------------------------------------------------------------//
#include <stdlib.h>
#include <stdio.h>
void *_osk_memsetw(void *, int, long);
char *_osk_itoa(int, char *, int);
char *_osk_ltoa(long, char *, int);
char *_osk_utoa(unsigned int, char *, int);
char *_osk_ultoa(unsigned long, char *, int);
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
// please don't mind how weird this file is
// I remove tests of "simple" functions after
// I'm done with testing that function
// so a lot of test variables remain in case
// I need them for a future test
//const char *test1 = "test string\n";
//const size_t size1 = strlen(test1);
//char *test2 = malloc(size1);
//char *test3 = malloc(size1);
#if 0
const size_t sizex = 130;
short *xxx = (short *)malloc(sizex * sizeof(short));
//printf("%ld\n",(ulong)xxx%8);
//memzero(xxx, sizex);
_osk_memsetw(xxx, 300, sizex);
size_t it;
for (it = 0; it < sizex; it++) {
short s = *(xxx + it);
printf("%hd", s);
}
free((void *)xxx);
#endif
char buf[256];
puts(_osk_ultoa(5000000000, buf, 10));
puts("");
//const char *str = "ceci est un string de test!";
//char *str2 = malloc((strlen(str) + 3) * sizeof(char));
//char *str3 = malloc((strlen(str) + 3) * sizeof(char));
//strcpy(str2, str);
//size_t s = strlen(str2);
//strrev(str3,str2);
//printf("%lu - %s\n", strlen(str3), str3);
//str2[s] = '\n';
//str2[s+1] = '\0';
//strrev(str2,str3);
//printf("%lu - %s\n", strlen(str2), str2);
//free(str2);
//free(str3);
//free(test2);
//free(test3);
return 0;
}

View File

@ -0,0 +1,28 @@
// random test file
#include <stdio.h>
#include <limits.h>
int main(int argc, char *argv[])
{
long x[4];
long *ptr = &x[0];
printf("%d\n", CHAR_MIN);
printf("%hhd\n", (1 << 7));
printf("%p\n", ptr);
ptr++;
printf("%p\n", ptr);
ptr = ptr + 1;
printf("%p\n", ptr);
ptr = (long *)((long)ptr + 1);
printf("%p\n", ptr);
return 0;
}

View File

@ -1,43 +0,0 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Essential types for Kaleid //
//----------------------------------------------------------------------------//
#ifndef _KALCOMM_TYPES_H
#define _KALCOMM_TYPES_H
#ifndef _KALCOMM_COMMON_H
#error "don't include common/types.h without common/common.h"
#endif
#ifndef KEEP_KALCOMM_TYPES_MINIMAL
typedef _Bool bool;
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef long long llong;
typedef unsigned long long ullong;
typedef long double ldouble;
typedef uint wchar_t;
typedef ullong size_t;
typedef llong ssize_t;
typedef size_t off_t;
typedef int atomic_t;
typedef ulong pid_t;
typedef void *va_list;
#endif
typedef short port_t;
typedef short status_t;
// XXX limits
#endif

View File

@ -0,0 +1,78 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Kaleid assert() support //
//----------------------------------------------------------------------------//
#ifndef _KALASSRT_H
#define _KALASSRT_H
//------------------------------------------//
// Macros //
//------------------------------------------//
#ifndef noreturn
#define noreturn __attribute__((__noreturn__))
#endif
#ifndef unlikely(x)
#define unlikely(x) (__builtin_expect((x), 0))
#endif
//------------------------------------------//
// When debugging //
//------------------------------------------//
#if !defined(_NO_DEBUG) && !defined(NDEBUG) && !defined(assert)
//
// Failed assert handler
//
noreturn void _assert_handler(const char *, const char *, int, const char *);
#define assert(x) \
do { \
if unlikely(!(x)) \
_assert_handler(#x, __FILE__, __LINE__, __func__); \
} while (0);
//------------------------------------------//
// When not debugging //
//------------------------------------------//
#else
#ifndef NDEBUG
#define NDEBUG 1
#endif
#ifndef _NO_DEBUG
#define _NO_DEBUG 1
#endif
#ifndef assert
#define assert(x)
#endif
#endif
//------------------------------------------//
// Aliases for assert() //
//------------------------------------------//
#ifndef Assert
#define Assert assert
#endif
#ifndef KalAssert
#define KalAssert assert
#endif
//------------------------------------------//
// End of <kalassrt.h> //
//------------------------------------------//
#endif

197
src/kaleid/include/kalcrt.h Normal file
View File

@ -0,0 +1,197 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Kaleid C runtime library //
//----------------------------------------------------------------------------//
#ifndef _KALCRT_H
#define _KALCRT_H
//------------------------------------------//
// Typedefs //
//------------------------------------------//
#ifndef __size_t
#define __size_t
typedef unsigned long size_t;
#endif
#ifndef __status_t
#define __status_t
typedef signed long status_t;
#endif
#ifndef __va_list
#define __va_list
typedef __builtin_va_list va_list;
#endif
#ifndef __div_t
#define __div_t
typedef struct { int quot, rem; } div_t;
#endif
#ifndef __ldiv_t
#define __ldiv_t
typedef struct { long quot, rem; } ldiv_t;
#endif
//------------------------------------------//
// Macros //
//------------------------------------------//
#ifndef _NO_MASK
#define _NO_MASK
#endif
//------------------------------------------//
// va_list utilities //
//------------------------------------------//
#ifndef va_start
#define va_start __builtin_va_start
#endif
#ifndef va_arg
#define va_arg __builtin_va_arg
#endif
#ifndef va_next
#define va_next __builtin_va_next
#endif
#ifndef va_end
#define va_end __builtin_va_end
#endif
//------------------------------------------//
// Memory management utilities //
//------------------------------------------//
#ifndef memset
#define memset memsetb
#endif
#ifndef memchr
#define memchr memchrb
#endif
void *memsetb(void *, int, size_t);
void *memsetw(void *, int, size_t);
void *memsetd(void *, int, size_t);
void *memsetq(void *, long, size_t);
void *memchrb(const void *, int, size_t);
void *memchrw(const void *, int, size_t);
void *memchrd(const void *, int, size_t);
void *memchrq(const void *, long, size_t);
void *memcpy(void *, const void *, size_t);
void *memmove(void *, const void *, size_t);
void *memzero(void *, size_t);
int memcmp(const void *, const void *, size_t);
//------------------------------------------//
// String manipulation utilities //
//------------------------------------------//
size_t strlen(const char *);
char *strcpy(char *, const char *);
char *strncpy(char *, const char *, size_t);
char *strrev(char *, const char *);
char *reverse(char *);
int sprintf(char *, const char *, ...);
int snprintf(char *, size_t, const char *, ...);
int vsprintf(char *, const char *, va_list);
int vsnprintf(char *, size_t, const char *, va_list);
//------------------------------------------//
// Type conversion utilities //
//------------------------------------------//
int *atoi(const char *);
long *atol(const char *);
char *itoa(int, char *, int);
char *ltoa(long, char *, int);
char *utoa(unsigned int, char *, int);
char *ultoa(unsigned long, char *, int);
long strtol(const char *, char **, int);
unsigned long strtoul(const char *, char **, int);
//------------------------------------------//
// RNG utilities //
//------------------------------------------//
int rand(void);
void srand(unsigned int);
//------------------------------------------//
// Diverse utilities //
//------------------------------------------//
const char *describe_status(status_t) _NO_MASK;
//------------------------------------------//
// Arithmetical macros //
//------------------------------------------//
#ifndef abs
#define abs(x) ((x) < 0 ? -x : x)
#endif
#ifndef labs
#define labs(x) ((x) < 0 ? -x : x)
#endif
#ifndef min
#define min(x,y) ((x) < (y) ? (x) : (y))
#endif
#ifndef lmin
#define lmin(x,y) ((x) < (y) ? (x) : (y))
#endif
#ifndef max
#define max(x,y) ((x) < (y) ? (x) : (y))
#endif
#ifndef lmax
#define lmax(x,y) ((x) < (y) ? (x) : (y))
#endif
#ifndef __div
#define __div
static inline div_t div(int __x, int __y)
{
div_t __res;
__res.quot = __x/__y;
__res.rem = __x%__y;
return __res;
}
#endif
#ifndef __ldiv
#define __ldiv
static inline ldiv_t ldiv(long __x, long __y)
{
ldiv_t __res;
__res.quot = __x/__y;
__res.rem = __x%__y;
return __res;
}
#endif
//------------------------------------------//
// End of <kalcrt.h> //
//------------------------------------------//
#endif

View File

@ -0,0 +1,89 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Kaleid general preprocessor constants //
//----------------------------------------------------------------------------//
#ifndef _KALDEFS_H
#define _KALDEFS_H
//------------------------------------------//
// Actual constants //
//------------------------------------------//
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef NULL
#define NULL ((void *)0)
#endif
#ifndef INITOK
#define INITOK ((unsigned int)0xCAFEBABE)
#endif
//------------------------------------------//
// Keywords and attributes //
//------------------------------------------//
#ifndef alignof
#define alignof _Alignof
#endif
#ifndef PACKED
#define PACKED __attribute__((__packed__))
#endif
#ifndef noreturn
#define noreturn __attribute__((__noreturn__))
#endif
#ifndef likely
#define likely(x) (__builtin_expect((x), 1))
#endif
#ifndef unlikely(x)
#define unlikely(x) (__builtin_expect((x), 0))
#endif
//------------------------------------------//
// API specific macros //
//------------------------------------------//
#ifndef KALAPI
# define KALAPI
#endif
//------------------------------------------//
// Values for status_t //
//------------------------------------------//
#define STATUS_FAILED(x) ((x) < 0))
#define STATUS_SUCCESS(x) (!STATUS_FAILED(x))
#define SUCCESS (0) // everything went fine
#define FAILED (-1)
#define ERROR FAILED // something went wrong
#define NOT_PERMITTED (-2)
#define ACCESS_DENIED (-3)
#define BAD_ARGUMENT (-4) // invalid arguments
#define BAD_ARG_RANGE (-5) // arguments out of range
#define BAD_ARG_NULL (-6) // unexpected NULL argument
#define TRY_AGAIN (-7) // EAGAIN
//------------------------------------------//
// End of <kaldefs.h> //
//------------------------------------------//
#endif

View File

@ -0,0 +1,53 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Kaleid API main include file //
//----------------------------------------------------------------------------//
#ifndef _KALEID_H
#define _KALEID_H
//------------------------------------------//
// Building for OS/K //
//------------------------------------------//
#if !defined(_OSK_SOURCE)
# if defined(_KALEID_KERNEL) || defined(_KALEID_SYSTEM)
# define _OSK_SOURCE 1
# endif
#endif
#if !defined(_OSK_SOURCE)
# ifndef _KALMASK_H
# include <kalmask.h>
# endif
#endif
//------------------------------------------//
// Include common part of API //
//------------------------------------------//
#ifndef _KALDEFS_H
#include <kaldefs.h>
#endif
#ifndef _KALTYPES_H
#include <kaltypes.h>
#endif
#ifndef _KALASSRT_H
#include <kalassrt.h>
#endif
#ifndef _KALCRT_H
#include <kalcrt.h>
#endif
//------------------------------------------//
// End of <kaleid.h> //
//------------------------------------------//
#endif

1
src/kaleid/include/kalkern Symbolic link
View File

@ -0,0 +1 @@
../kernel

View File

@ -0,0 +1,37 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Kaleid Kernel main include file //
//----------------------------------------------------------------------------//
#ifndef _KALKERN_H
#define _KALKERN_H
//------------------------------------------//
// Dependencies //
//------------------------------------------//
#ifndef _KALEID_H
#include <kaleid.h>
#endif
#ifndef _KALKERN_CONFIG_H
#include <kalkern/config.h>
#endif
//------------------------------------------//
// Macros //
//------------------------------------------//
#define DisableInterrupts() asm volatile ("cli")
#define EnableInterrupts() asm volatile ("sti")
#define HaltCPU() asm volatile ("hlt")
//------------------------------------------//
// End of <kalkern.h> //
//------------------------------------------//
#endif

View File

@ -0,0 +1,116 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Kaleid type limits definitions //
//----------------------------------------------------------------------------//
#ifndef _KALLIMS_H
//------------------------------------------//
// Data sizes blocks //
//------------------------------------------//
#ifndef DATA_SIZE_BLOCK
#define DATA_SIZE_BLOCK
# define BYTE_SIZE sizeof(char)
# define WORD_SIZE sizeof(short)
# define DWORD_SIZE sizeof(int)
# define QWORD_SIZE sizeof(long)
#endif
#ifndef DATA_ALIGN_BLOCK
#define DATA_ALIGN_BLOCK
# define BYTE_ALIGN alignof(char)
# define WORD_ALIGN alignof(short)
# define DWORD_ALIGN alignof(int)
# define QWORD_ALIGN alignof(long)
#endif
#ifndef DATA_BITS_BLOCK
#define DATA_BITS_BLOCK
# define BYTE_BIT 8
# define CHAR_BIT (BYTE_SIZE * BYTE_BIT)
# define WORD_BIT (WORD_SIZE * BYTE_BIT)
# define DWORD_BIT (DWORD_SIZE * BYTE_BIT)
# define QWORD_BIT (QWORD_SIZE * BYTE_BIT)
# define SHORT_BIT WORD_BIT
# define INT_BIT DWORD_BIT
# define LONG_BIT QWORD_BIT
#endif
#ifndef DATA_SHIFTS_BLOCK
#define DATA_SHIFTS_BLOCK
# define BYTES_TO_WORDS(B) ((B) >> 1)
# define BYTES_TO_DWORDS(B) ((B) >> 2)
# define BYTES_TO_QWORDS(B) ((B) >> 3)
# define WORDS_TO_BYTES(W) ((W) << 1)
# define WORDS_TO_DWORDS(W) ((W) >> 1)
# define WORDS_TO_QWORDS(W) ((W) >> 2)
# define DWORDS_TO_BYTES(D) ((D) << 2)
# define DWORDS_TO_WORDS(D) ((D) << 1)
# define DWORDS_TO_QWORDS(D) ((D) >> 1)
# define QWORDS_TO_BYTES(Q) ((Q) << 3)
# define QWORDS_TO_WORDS(Q) ((Q) << 2)
# define QWORDS_TO_DWORDS(Q) ((Q) << 1)
#endif
//------------------------------------------//
// Numeric data limits //
//------------------------------------------//
#ifndef DATA_MAX_LIMITS_BLOCK
#define DATA_MAX_LIMITS_BLOCK
# define SCHAR_MAX ((signed char) 0x7F)
# define SHRT_MAX ((short) 0x7FFF)
# define INT_MAX ((int) 0x7FFFFFFF)
# define LONG_MAX ((long) 0x7FFFFFFFFFFFFFFF)
# define UCHAR_MAX ((unsigned char) 0xFF
# define USHRT_MAX ((unsigned short) 0xFFFF)
# define UINT_MAX ((unsigned int) 0xFFFFFFFF)
# define ULONG_MAX ((unsigned long) 0xFFFFFFFFFFFFFFFF)
#endif
#ifndef DATA_MIN_LIMITS_BLOCK
#define DATA_MIN_LIMITS_BLOCK
# define SCHAR_MIN ((signed char) -SCHAR_MAX - 1)
# define SHRT_MIN ((short) -SHRT_MAX - 1)
# define INT_MIN ((int) -INT_MAX - 1)
# define LONG_MIN ((long) -LONG_MAX - 1L)
#endif
#ifndef DATA_CHAR_LIMITS_BLOCK
#define DATA_CHAR_LIMITS_BLOCK
# ifdef __CHAR_UNSIGNED__
# define CHAR_MIN ((char)0)
# define CHAR_MAX UCHAR_MAX
# else
# define CHAR_MIN SCHAR_MIN
# define CHAR_MAX SCHAR_MAX
# endif
#endif
#ifndef DATA_SPTYPES_LIMITS_BLOCK
#define DATA_SPTYPES_LIMITS_BLOCK
# define SSIZE_T_MIN LONG_MIN
# define SSIZE_T_MAX LONG_MAX
# define SIZE_T_MAX ULONG_MAX
#endif
#ifdef NEED_MORE_USELESS_DATA
# define UCHAR_MIN ((unsigned char)0)
# define USHRT_MIN ((unsigned short)0)
# define UINT_MIN ((unsigned int)0)
# define ULONG_MIN ((unsigned long)0)
# ifdef STILL_NEED_MORE_USELESS_DATA
# error "Not enough useless data!"
# endif
#endif
//------------------------------------------//
// End of <kallims.h> //
//------------------------------------------//
#endif

View File

@ -0,0 +1,87 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Masks for the functions in the KCRL //
//----------------------------------------------------------------------------//
#ifndef _KALMASK_H
#define _KALMASK_H
//------------------------------------------//
#define memset _osk_memsetb
#define memchr _osk_memchrb
#define memsetb _osk_memsetb
#define memsetw _osk_memsetw
#define memsetd _osk_memsetd
#define memsetq _osk_memsetq
#define memchrb _osk_memchrb
#define memchrw _osk_memchrw
#define memchrd _osk_memchrd
#define memchrq _osk_memchrq
#define memcpy _osk_memcpy
#define memmove _osk_memmove
#define memcmp _osk_memcmp
#define memzero _osk_memzero
//------------------------------------------//
#define strlen _osk_strlen
#define strcpy _osk_strcpy
#define strncpy _osk_strncpy
#define strrev _osk_strrev
#define reverse _osk_reverse
#define sprintf _osk_sprintf
#define snprintf _osk_snprintf
#define vsprintf _osk_vsprintf
#define vsnprintf _osk_vsnprintf
//------------------------------------------//
#define atoi _osk_atoi
#define atol _osk_atol
#define itoa _osk_itoa
#define ltoa _osk_ltoa
#define utoa _osk_utoa
#define ultoa _osk_ultoa
#define strtol _osk_strtol
#define strtoul _osk_strtoul
//------------------------------------------//
#define rand _osk_rand
#define srand _osk_srand
//------------------------------------------//
#define abs _osk_abs
#define labs _osk_labs
#define min _osk_min
#define lmin _osk_lmin
#define max _osk_max
#define lmax _osk_lmax
#define __div
#define __ldiv
#define div _osk_div
#define ldiv _osk_ldiv
//------------------------------------------//
// End of <kalmask.h> //
//------------------------------------------//
#endif

View File

@ -0,0 +1,86 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Kaleid C common types //
//----------------------------------------------------------------------------//
#ifndef _KALTYPES_H
#define _KALTYPES_H
//------------------------------------------//
// Basic integer types aliases //
//------------------------------------------//
#ifndef __base_types_aliases
#define __base_types_aliases
typedef unsigned char uchar;
typedef signed char schar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef signed long long llong;
typedef unsigned long long ullong;
typedef long double ldouble;
#endif
//------------------------------------------//
// Other standard integer types //
//------------------------------------------//
#ifndef __size_t
#define __size_t
typedef unsigned long size_t;
#endif
#ifndef __ssize_t
#define __ssize_t
typedef signed long ssize_t;
#endif
#ifndef __wchar_t
#define __wchar_t
typedef signed int wchar_t;
#endif
#ifndef __off_t
#define __off_t
typedef unsigned long off_t;
#endif
//------------------------------------------//
// Special types //
//------------------------------------------//
#ifndef __va_list
#define __va_list
typedef __builtin_va_list va_list;
#endif
#ifndef __div_t
#define __div_t
typedef struct { int quot, rem; } div_t;
#endif
#ifndef __ldiv_t
#define __ldiv_t
typedef struct { long quot, rem; } ldiv_t;
#endif
//------------------------------------------//
// Kaleid-specific types //
//------------------------------------------//
#ifndef __status_t
#define __status_t
typedef signed long status_t;
#endif
//------------------------------------------//
// End of <kaltypes.h> //
//------------------------------------------//
#endif

View File

@ -23,12 +23,12 @@
// //
// Enable/disable multiprocessor support // Enable/disable multiprocessor support
// //
#define MULTIPROCESSOR NO #define MULTIPROCESSOR FALSE
// //
// Enable/disable preemptivity // Enable/disable preemptivity
// //
#define PREEMPTIVE YES #define PREEMPTIVE TRUE
// //
// Size of a tabulation in spaces // Size of a tabulation in spaces

View File

@ -1,44 +1,37 @@
//----------------------------------------------------------------------------// ---------------------------------------------------------------------
// GNU GPL OS/K // GNU GPL OS/K
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Folder description - "kaleid/kernel" //
//----------------------------------------------------------------------------//
This is the folder containing the source of Kaleid's kernel. Authors: spectral`
NeoX
It contains the following files: Desc: Folder description - "kaleid/kernel"
---------------------------------------------------------------------
This folder contains the source of Kaleid's kernel component.
This contains the following files:
- init.c - init.c
The file containing the entry point of Kaleid, the kstart() function The file containing the entry point of Kaleid, the kstart() function
called from the bootloader (see ../../boot). called from the bootloader (see ../../boot).
This folder also has the following subfolders This folder also has the following subfolders:
(for more information on a particular subfolder, see {name}/folder.desc)
- mm/ - mm/
This folder contains all files related to memory management. This folder contains all files related to memory management.
- fs/ - fs/
This folder contains Kaleid's virtual filesystem, as well as one This folder will contain Kaleid's virtual filesystem, as well as one
subfolder for each FS supported by Kaleid (e.g. FAT filesystem). subfolder for each FS supported by Kaleid (e.g. FAT filesystem).
- io/ - io/
I/O folder. (XXX) I/O folder. (XXX)
- ps/ - ps/
This folder contains Kaleid's process manager and scheduler, as well as the This folder will contain Kaleid's process manager and scheduler, as well
implementation of the related syscalls and functions. as the implementation of the related syscalls and functions.
- ex/ - ex/
This folder contains the exec()-related functions and syscalls, as well This folder contains the exec()-related functions and syscalls, as well
as one subfolder per executable format supported by Kaleid (e.g. ELF executable) as one subfolder per executable format supported by Kaleid
(e.g. ELF executable)
- se/
Security folder. (XXX)
- sys/
Syscall folder. (XXX)

View File

@ -7,16 +7,26 @@
// Desc: Kernel entry point // // Desc: Kernel entry point //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include "kernel/init.h" #include <kalkern/init.h>
#include "kernel/ke/panic.h" #include <kalkern/ke/state.h>
#include "kernel/io/terminal.h" #include <kalkern/ke/panic.h>
#include <kalkern/io/terminal.h>
// //
// Entry point of kaleid-kernel.elf // Entry point of kaleid-kernel.elf
// //
void kstart(void) void StartKern(void)
{ {
kterm_init(); // we're not ready to deal with interrupts
panic("Goodbye World :("); DisableInterrupts();
// booting!
SetKernState(KSTATE_INIT);
// kernel terminals
InitTerms();
// we're out
StartPanic("Goodbye World :(");
} }

View File

@ -10,10 +10,12 @@
#ifndef _KALKERN_INIT_H #ifndef _KALKERN_INIT_H
#define _KALKERN_INIT_H #define _KALKERN_INIT_H
#include "common/common.h" #ifndef _KALKERN_H
#include <kalkern.h>
#endif
// kernel entry point // kernel entry point
void kstart(void); void StartKern(void);
#endif #endif

View File

@ -7,7 +7,7 @@
// Desc: Ports I/O // // Desc: Ports I/O //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include "kernel/io/ports.h" #include <kalkern/io/ports.h>

View File

@ -10,12 +10,14 @@
#ifndef _KALKERN_IO_PORTS_H #ifndef _KALKERN_IO_PORTS_H
#define _KALKERN_IO_PORTS_H #define _KALKERN_IO_PORTS_H
#include "common/common.h" #ifndef _KALKERN_H
#include <kalkern.h>
#endif
#define outb(port,val) asm volatile ("outb %1, %0" : : "dN" (port), "a" (value)) #define WriteByteOnPort(port,val) asm volatile ("outb %1, %0" : : "dN" (port), "a" (value))
uchar inb(port_t); uchar ReadByteFromPort(port_t);
ushort inw(port_t); ushort ReadWordFromPort(port_t);
#endif #endif

View File

@ -7,78 +7,68 @@
// Desc: Early terminal functions // // Desc: Early terminal functions //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include "kernel/io/terminal.h" #define _UNLOCKED_IO
#include <kalkern/io/terminal.h>
// //
// VGA-related macros // VGA-related macros
// //
#define ComputeColorCode(fg, bg) ((fg) | (bg) << 4) #define ComputeColorCode(fg, bg) ((fg) | (bg) << 4)
#define ComputeEntryOffset(kt, x, y) ((y) * kt->kt_width + (x)) #define ComputeOffset(kt, x, y) ((y) * kt->kt_width + (x))
#define ComputeEntry(ch, cl) (((ushort)(ch)) | (ushort)(cl) << 8) #define ComputeEntry(ch, cl) (((ushort)(ch)) | (ushort)(cl) << 8)
// //
// VGA output // VGA output
// //
static struct kterm _kt_vga = { static terminal_t _vga_term = {
.kt_buffer = (ushort *)0xB8000, .kt_buffer = (ushort *)0xB8000,
.kt_width = 80, .kt_width = 80,
.kt_height = 25, .kt_height = 25,
.kt_curr_x = 0, .kt_curr_x = 0,
.kt_curr_y = 0, .kt_curr_y = 0,
.kt_color = ComputeColorCode(KTERM_COLOR_LGREY, KTERM_COLOR_BLACK), .kt_color = ComputeColorCode(KTERM_COLOR_LGREY, KTERM_COLOR_BLACK),
.kt_lock = NULL, .kt_lock = INITLOCK(KLOCK_MUTEX),
#ifndef _NO_DEBUG
.kt_init = FALSE, .kt_init = FALSE,
#endif
}; };
// //
// Standard output terminal // Standard output terminal
// //
struct kterm *kt_stdout; terminal_t *stdout;
//
// Debugging terminal
//
terminal_t *stddbg;
// //
// Initialize standard output // Initialize standard output
// //
void kterm_init(void) void InitTerms(void)
{ {
assert(!kt_stdout && !_kt_vga.kt_init && "kterm_init() called twice"); Assert(!stdout && _vga_term.kt_init != INITOK);
#ifndef _NO_DEBUG _vga_term.kt_init = INITOK;
_kt_vga.kt_init = TRUE; stddbg = &_vga_term;
#endif
// to be switched to VESA // to be switched to VESA
kt_stdout = &_kt_vga; stdout = &_vga_term;
ktclear(); ClearTerm(stdout);
} }
// //
// Fills terminal with spaces // Fill terminal with spaces
// XXX would '\0' work too?
// //
status_t kterm_clear(struct kterm *kt) status_t ClearTerm(terminal_t *kt)
{ {
size_t i;
if (kt == NULL) if (kt == NULL)
return BAD_ARG_NULL; return BAD_ARG_NULL;
assert(kt->kt_init && "kterm_clear called before initialization"); Assert(kt->kt_init == INITOK);
kterm_lock(kt); LockTerm(kt);
ClearTermUnlocked(kt);
const ushort filler = ComputeEntry(' ', kt->kt_color); UnlockTerm(kt);
const size_t bufsize = kt->kt_width * kt->kt_height;
for (i = 0; i < bufsize; i++) {
// XXX implement memset()
kt->kt_buffer[i] = filler;
}
kt->kt_curr_x = kt->kt_curr_y = 0;
kterm_unlock(kt);
return SUCCESS; return SUCCESS;
} }
@ -86,7 +76,7 @@ status_t kterm_clear(struct kterm *kt)
// //
// Change the color code // Change the color code
// //
status_t kterm_change_color(struct kterm *kt, uchar color) status_t ChTermColor(terminal_t *kt, uchar color)
{ {
if (color > KTERM_COLOR_WHITE) if (color > KTERM_COLOR_WHITE)
return BAD_ARG_RANGE; return BAD_ARG_RANGE;
@ -94,21 +84,79 @@ status_t kterm_change_color(struct kterm *kt, uchar color)
if (kt == NULL) if (kt == NULL)
return BAD_ARG_NULL; return BAD_ARG_NULL;
kterm_lock(kt); LockTerm(kt);
kt->kt_color = color; kt->kt_color = color;
kterm_unlock(kt); UnlockTerm(kt);
return SUCCESS; return SUCCESS;
} }
// //
// Writes a single character on the terminal (UNLOCKED version) // Write a single character on the terminal
// //
// DEPRECATED: status_t PutOnTerm(terminal_t *kt, char ch)
// - always use kterm_putch (LOCKED version) {
// - doesn't check for NULL input if (kt == NULL)
return BAD_ARG_NULL;
Assert(kt->kt_init == INITOK);
LockTerm(kt);
PutOnTermUnlocked(kt, ch);
UnlockTerm(kt);
return SUCCESS;
}
// //
void kterm_putch_unlocked(struct kterm *kt, char ch) // Print string on terminal
//
status_t PrintOnTerm(terminal_t *kt, const char *str)
{
if (kt == NULL)
return BAD_ARG_NULL;
Assert(kt->kt_init == INITOK);
LockTerm(kt);
while (*str) {
PutOnTermUnlocked(kt, *str++);
}
UnlockTerm(kt);
return SUCCESS;
}
//----------------------------------------------------------//
// UNLOCKED VERSIONS //
// //
// Direct use is highly deprecated //
// Useful in rare instances //
//----------------------------------------------------------//
//
// Fill terminal with spaces (UNLOCKED version)
// XXX would '\0' work too?
//
void ClearTermUnlocked(terminal_t *kt)
{
size_t i;
const ushort filler = ComputeEntry(' ', kt->kt_color);
const size_t bufsize = kt->kt_width * kt->kt_height;
for (i = 0; i < bufsize; i++) {
// XXX implement memsetw()
kt->kt_buffer[i] = filler;
}
kt->kt_curr_x = kt->kt_curr_y = 0;
}
//
// Write a single character on the terminal (UNLOCKED version)
//
void PutOnTermUnlocked(terminal_t *kt, char ch)
{ {
int i; int i;
size_t prev_row; size_t prev_row;
@ -121,21 +169,21 @@ void kterm_putch_unlocked(struct kterm *kt, char ch)
// later in this function we actually do the line feed // later in this function we actually do the line feed
else if (ch == '\n') { kt->kt_curr_y = kt->kt_width - 1; } else if (ch == '\n') { kt->kt_curr_y = kt->kt_width - 1; }
// tabulations account for 4 spaces // tabulations account for "TABSIZE" spaces
else if (ch == '\t') { else if (ch == '\t') {
prev_row = kt->kt_curr_y; prev_row = kt->kt_curr_y;
// compiler will optimize this away // compiler will optimize this away
for (i = 0; i < TABSIZE; i++) { for (i = 0; i < TABSIZE; i++) {
// tabulations can't spread over two lines // tabulations can't spread over two lines
if (kt->kt_curr_y == prev_row) { if (kt->kt_curr_y == prev_row) {
kterm_putch_unlocked(kt, ' '); PutOnTermUnlocked(kt, ' ');
} }
} }
} }
// XXX check whether we were given a writable character
else { else {
const size_t offset = ComputeEntryOffset(kt, kt->kt_curr_x, kt->kt_curr_y); // actually write on the buffer
const size_t offset = ComputeOffset(kt, kt->kt_curr_x, kt->kt_curr_y);
kt->kt_buffer[offset] = ComputeEntry(ch, kt->kt_color); kt->kt_buffer[offset] = ComputeEntry(ch, kt->kt_color);
} }
@ -151,36 +199,12 @@ void kterm_putch_unlocked(struct kterm *kt, char ch)
} }
// //
// Writes a single character on the terminal (LOCKED version) // Print string on terminal (UNLOCKED version)
// //
status_t kterm_putch(struct kterm *kt, char ch) void PrintOnTermUnlocked(terminal_t *kt, const char *str)
{ {
if (kt == NULL)
return BAD_ARG_NULL;
kterm_lock(kt);
kterm_putch_unlocked(kt, ch);
kterm_unlock(kt);
return SUCCESS;
}
//
// Print string on kterminal
//
status_t kterm_print(struct kterm *kt, const char *str)
{
if (kt == NULL)
return BAD_ARG_NULL;
kterm_lock(kt);
while (*str) { while (*str) {
kterm_putch_unlocked(kt, *str++); PutOnTermUnlocked(kt, *str++);
} }
kterm_unlock(kt);
return SUCCESS;
} }

View File

@ -10,10 +10,10 @@
#ifndef _KALKERN_IO_KTERM_H #ifndef _KALKERN_IO_KTERM_H
#define _KALKERN_IO_KTERM_H #define _KALKERN_IO_KTERM_H
#include "common/common.h" #include <kalkern/ke/lock.h>
// all available colors // all available colors
enum kterm_color { enum terminal_colors {
KTERM_COLOR_BLACK, KTERM_COLOR_BLUE, KTERM_COLOR_BLACK, KTERM_COLOR_BLUE,
KTERM_COLOR_GREEN, KTERM_COLOR_CYAN, KTERM_COLOR_GREEN, KTERM_COLOR_CYAN,
KTERM_COLOR_RED, KTERM_COLOR_MAGENTA, KTERM_COLOR_RED, KTERM_COLOR_MAGENTA,
@ -24,41 +24,46 @@ enum kterm_color {
KTERM_COLOR_LBROWN, KTERM_COLOR_WHITE KTERM_COLOR_LBROWN, KTERM_COLOR_WHITE
}; };
struct kterm { typedef struct {
void *kt_lock; lock_t kt_lock;
ushort *kt_buffer; ushort *kt_buffer;
uchar kt_color; uchar kt_color;
size_t kt_width; size_t kt_width;
size_t kt_height; size_t kt_height;
off_t kt_curr_x; off_t kt_curr_x;
off_t kt_curr_y; off_t kt_curr_y;
#ifndef _NO_DEBUG uint kt_init;
bool kt_init; // XXX flags
#endif } terminal_t;
};
// current "standard" terminal // current "standard" terminal
extern struct kterm *kt_stdout; extern terminal_t *stdout;
void kterm_init(void); // current debugging terminal
status_t kterm_clear(struct kterm *); extern terminal_t *stddbg;
status_t kterm_putch(struct kterm *, char);
status_t kterm_print(struct kterm *, const char *);
status_t kterm_change_color(struct kterm *, uchar);
#ifdef _UNLOCKED_IO void InitTerms(void);
void kterm_putch_unlocked(struct kterm *, char); status_t ClearTerm(terminal_t *);
status_t PutOnTerm(terminal_t *, char);
status_t PrintOnTerm(terminal_t *, const char *);
status_t ChTermColor(terminal_t *, uchar);
#if defined(_UNLOCKED_IO)
void ClearTermUnlocked(terminal_t *);
void PutOnTermUnlocked(terminal_t *, char);
void PrintOnTermUnlocked(terminal_t *, const char *);
#define ChTermColorUnlocked(kt, col) ((kt)->kt_color = (col))
#endif #endif
#define ktclear() kterm_clear(kt_stdout) #ifndef _NO_DEBUG
#define ktputch(c) kterm_putch(kt_stdout, (c)) # define DebugLog(...) PrintOnTerm(stddbg, __VA_ARGS__)
#define ktprint(s) kterm_print(kt_stdout, (s)) #else
#define ktchcol(c) kterm_change_color(kt_stdout, (c)) # define DebugLog(...)
#endif
#define kterm_lock(kt) #define LockTerm(kt) AquireLock(&(kt)->kt_lock)
#define kterm_trylock(kt) #define UnlockTerm(kt) ReleaseLock(&(kt)->kt_lock)
#define kterm_unlock(kt) #define TryLockTerm(kt) AttemptLock(&(kt)->kt_lock)
#endif #endif

View File

@ -4,8 +4,10 @@
// Authors: spectral` // // Authors: spectral` //
// NeoX // // NeoX //
// // // //
// Desc: mem*() functions // // Desc: Locks //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include "common/memory.h" #include <kalkern/ke/lock.h>
// nothing to do here

View File

@ -0,0 +1,98 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Locks //
//----------------------------------------------------------------------------//
#ifndef _KALKERN_KE_LOCK_H
#define _KALKERN_KE_LOCK_H
#ifndef _KALKERN_H
#include <kalkern.h>
#endif
enum lock_type {
//
// Mutex-type lock
//
// WARNING
// AquireLock() panics when used on a mutex while not running a process
//
KLOCK_MUTEX,
//
// Spinlock-type lock
// Turns into a Mutex-type lock when MULTIPROCESSOR is off
//
KLOCK_SPINLOCK,
};
typedef struct {
uchar lk_type; // lock type?
uint lk_lock; // is locked?
void *lk_owner; // unused
void *lk_waiting; // unused
uint lk_init; // unused if _NO_DEBUG
} lock_t;
//
// Initialize a lock
//
#define InitLock(lk, type) \
do { \
(lk)->lk_type = (type); \
(lk)->lk_lock = FALSE; \
(lk)->lk_owner = NULL; \
(lk)->lk_waiting = NULL; \
(lk)->lk_init = INITOK; \
} while (FALSE);
//
// Alternative way to initalize a lock
//
#define INITLOCK(type) { (type), FALSE, NULL, NULL, INITOK }
//
// Does nothing
//
#define DestroyLock(lk) ((lk)->lk_init = FALSE)
//
// Aquires the lock
// Panics on double aquisition since that should never happen
// until we have at least a basic scheduler
//
#define AquireLock(lk) \
do { \
DisableInterrupts(); \
Assert((lk)->lk_init == INITOK); \
if ((lk)->lk_lock++) \
StartPanic("DosAquireLock on an already locked object"); \
EnableInterrupts(); \
} while (FALSE);
//
// Releases an already aquired lock
// Panics if the lock was never aquired (this will change)
//
#define ReleaseLock(lk) \
do { \
DisableInterrupts(); \
Assert((lk)->lk_init == INITOK); \
if ((lk)->lk_lock++) \
StartPanic("DosReleaseLock on an unlocked object"); \
EnableInterrupts(); \
} while (FALSE);
//
// Tries to aquire lock
// Doesn't work at all for obvious reasons
//
#define AttemptLock(lk) ((lk)->lk_lock++)
#endif

View File

@ -7,53 +7,62 @@
// Desc: How NOT to panic 101 // // Desc: How NOT to panic 101 //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#include "kernel/ke/panic.h" #include <kalkern/ke/panic.h>
#include <kalkern/ke/state.h>
#define _UNLOCKED_IO
#include <kalkern/io/terminal.h>
// //
// Panic message // Panic message
// //
const char *panicstr = NULL; const char *__panicmsg = NULL;
// //
// Failed assert() handler // Failed assert() handler
// //
noreturn void ___assert_handler(const char *msg, const char *file, int line, const char *func) noreturn void _assert_handler(const char *msg,
const char *file,
int line,
const char *func)
{ {
// not getting out of here // not getting out of here
cli(); DisableInterrupts();
(void)file; (void)line; (void)func; (void)file; (void)line; (void)func;
// XXX sprintf() to create a proper panicstr // XXX sprintf() to create a proper panicstr
panic(msg); StartPanic(msg);
} }
// //
// Your best boy panic() // Your best boy panic()
// //
void panic(const char *str) void StartPanic(const char *str)
{ {
cli(); DisableInterrupts();
ktclear(); SetKernState(KSTATE_PANIC);
ClearTermUnlocked(stdout);
if (str == NULL) { if (str == NULL) {
str = "(no message given)"; str = "(no message given)";
} }
if (panicstr) { if (GetPanicStr()) {
// shouldn't be possible PrintOnTermUnlocked(stdout, "double panic!\n");
ktprint("double panic!\n"); HaltCPU();
hlt();
} }
panicstr = str; SetPanicStr(str);
ktprint("panic! - "); // we cannot lock anything when panicking
ktprint(str); PrintOnTermUnlocked(stdout, "panic! - ");
PrintOnTermUnlocked(stdout, str);
while (TRUE) { while (TRUE) {
hlt(); HaltCPU();
} }
} }

View File

@ -10,9 +10,15 @@
#ifndef _KALKERN_KE_PANIC_H #ifndef _KALKERN_KE_PANIC_H
#define _KALKERN_KE_PANIC_H #define _KALKERN_KE_PANIC_H
#include "kernel/io/terminal.h" #ifndef _KALKERN_H
#include <kalkern.h>
#endif
extern const char *panicstr; noreturn void StartPanic(const char *);
noreturn void panic(const char *); noreturn void CrashSystem(void);
extern const char *__panicmsg;
#define GetPanicStr() (__panicmsg)
#define SetPanicStr(str) (__panicmsg = (str))
#endif #endif

View File

@ -4,15 +4,13 @@
// Authors: spectral` // // Authors: spectral` //
// NeoX // // NeoX //
// // // //
// Desc: mem*() functions // // Desc: Current kernel state //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#ifndef _KALCOMM_MEMORY_H #include <kalkern/ke/state.h>
#define _KALCOMM_MEMORY_H
#ifndef _KALCOMM_COMMON_H //
#include "common/common.h" // Current kernel state
#endif //
uchar __kstate;
#endif

View File

@ -4,26 +4,34 @@
// Authors: spectral` // // Authors: spectral` //
// NeoX // // NeoX //
// // // //
// Desc: Atomic stuff // // Desc: Current kernel state //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#ifndef _KALCOMM_ATOMIC_H #ifndef _KALKERN_KE_STATE_H
#define _KALCOMM_ATOMIC_H #define _KALKERN_KE_STATE_H
#ifndef _KALCOMM_COMMON_H #ifndef _KALKERN_H
#error "don't include common/types.h without common/common.h" #include <kalkern.h>
#endif #endif
// atomic_t defined in common/types.h // XXX improve this
enum kernel_states {
// a process is running in kernel mode
KSTATE_PROCESS,
#ifdef _KALEID_KERNEL // the kernel is not running a process
KSTATE_KERNEL,
// only available in the kernel // the kernel is panicking
#define cli() asm volatile ("cli") KSTATE_PANIC,
#define sti() asm volatile ("sti")
#define hlt() asm volatile ("hlt") // the kernel is booting
KSTATE_INIT,
#endif };
extern uchar __kstate;
#define GetKernState() (__kstate)
#define SetKernState(x) (__kstate = (x))
#endif #endif

View File

@ -1,45 +0,0 @@
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: Test file for common/ //
//----------------------------------------------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KEEP_KALCOMM_TYPES_MINIMAL
#include "common/common.h"
#include "common/string.h"
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
const char *test1 = "test string\n";
char *test2 = malloc(strlen(test1));
char *test3 = malloc(strlen(test1));
printf("1\n");
#undef strlen
assert(strlen("test string") == _osk_strlen("test string"));
#undef strcpy
assert(strcmp(strcpy(test2, test1), _osk_strcpy(test3, test1)) == 0);
// tests done
printf("2\n");
free(test2);
free(test3);
return 0;
}

View File

@ -1,4 +1,4 @@
ENTRY(kstart) ENTRY(StartKern)
SECTIONS SECTIONS
{ {
. = 0x4000; /* XXX 0x4000 is temporary */ . = 0x4000; /* XXX 0x4000 is temporary */

View File

33
src/preproc.h Normal file
View File

@ -0,0 +1,33 @@
// be careful with this file
#ifdef _TESTS
# define CCC TCC
#else
# define CCC KCC
#endif
#ifdef _TO_ASM
# define _CSPREF -S
# define _OUTFIX S
# define LINK_KERNEL(out)
#else
# define _CSPREF -c
# define _OUTFIX o
# define LINK_KERNEL(out) $(KCC) $(CLDSCR) $(COMMOBJS) $(KERNOBJS) -o $(BINDIR)/out
#endif
#define COMPILE_CONVRT(file) $(CCC) _CSPREF $(COMMDIR)/convert.c -o $(COBJDIR)/file._OUTFIX
#define COMPILE_COMMON(file) $(CCC) _CSPREF $(COMMDIR)/file.c -o $(COBJDIR)/file._OUTFIX
#define COMPILE_KERNEL(file) $(KCC) _CSPREF $(KERNDIR)/file.c -o $(KOBJDIR)/file._OUTFIX
#define COBJ1(x1) $(COBJDIR)/x1.o
#define COBJ2(x1,x2) $(COBJDIR)/x1.o $(COBJDIR)/x2.o
#define COBJ3(x1,x2,x3) $(COBJDIR)/x1.o $(COBJDIR)/x2.o $(COBJDIR)/x3
#define COBJ4(x1,x2,x3,x4) $(COBJDIR)/x1.o $(COBJDIR)/x2.o $(COBJDIR)/x3.o $(COBJDIR)/x4.o
#define COBJ5(x1,x2,x3,x4,x5) $(COBJDIR)/x1.o $(COBJDIR)/x2.o $(COBJDIR)/x3.o $(COBJDIR)/x4.o $(COBJDIR)/x5.o
#define KOBJ1(x1) $(KOBJDIR)/x1.o
#define KOBJ2(x1,x2) $(KOBJDIR)/x1.o $(KOBJDIR)/x2.o
#define KOBJ3(x1,x2,x3) $(KOBJDIR)/x1.o $(KOBJDIR)/x2.o $(KOBJDIR)/x3
#define KOBJ4(x1,x2,x3,x4) $(KOBJDIR)/x1.o $(KOBJDIR)/x2.o $(KOBJDIR)/x3.o $(KOBJDIR)/x4.o
#define KOBJ5(x1,x2,x3,x4,x5) $(KOBJDIR)/x1.o $(KOBJDIR)/x2.o $(KOBJDIR)/x3.o $(KOBJDIR)/x4.o $(KOBJDIR)/x5.o

View File

@ -8,6 +8,13 @@
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
src/ src/
|
x COPYING
x CONTACT
x ChangeLog
|
- Makefile
- kernel.ld
| |
+ boot/ + boot/
| | | |
@ -20,39 +27,75 @@ src/
| - loader16.inc | - loader16.inc
| - loader64.inc | - loader64.inc
| | | |
| - types.h | 0
| |
+ kaleid/ + kaleid/
| | | |
| + include/
| | |
| | - kaleid.h
| | - kaldefs.h
| | - kaltypes.h
| | - kalassrt.h
| | - kalmask.h
| | - kalcrt.h
| | |
| | - kalkern.h
| | |
| | ~ kalkern/ -> ../kernel/
| | |
| | 0
| |
| |
| + kernel/ | + kernel/
| | | | | |
| | x folder.desc | | x folder.desc
| | | | | |
| | - config.h
| | - config.h.in
| | |
| | - init.c | | - init.c
| | - init.h | | - init.h
| | | | | |
| | + io/ | | + io/
| | | | | | |
| | - ports.c | | | - ports.c
| | - ports.h | | | - ports.h
| | - terminal.c | | | |
| | - terminal.h | | | - terminal.c
| | | - terminal.h
| | | |
| | | 0
| | |
| | + ke/
| | | |
| | | - lock.c
| | | - lock.h
| | | |
| | | - panic.c
| | | - panic.h
| | | |
| | | - state.c
| | | - state.h
| | | |
| | | 0
| | |
| | 0
| | | |
| + common/ | + common/
| | | | |
| x folder.desc | | - status.c
| | | | |
| - assert.h | | - string.c
| - atomic.h | | - memory.c
| - common.h | | - convert.c
| - config.h | | - sprintf.c
| - config.h.in | | |
| - status.h | | + test/
| - string.h | | | |
| - types.h | | | - test-common.c
| | | | | |
| + lib/ | | | 0
| | | | 0
| - string.c | 0
|
0 0

View File

@ -1,4 +0,0 @@
make
cp ../bin/bootloader.bin "~/Documents/GNU OSK/bin/bootloader.bin"
qemu-system-x86_64 -hda "~/Documents/GNU OSK/bin/bootloader.bin"
qemu-system-i386 -hda "~/Documents/GNU OSK/bin/bootloader.bin"