diff --git a/src/Makefile b/src/Makefile index 79c8836..cf8c31f 100644 --- a/src/Makefile +++ b/src/Makefile @@ -10,12 +10,13 @@ CCNAME="/opt/cross-cc/bin/x86_64-elf-gcc" CC2NAME=gcc # compiler for testing CLDSCR=-T kernel.ld -CWARNS= -pedantic -Wall -Wextra -Werror +COPTIM=-O2 #-fbuiltin-memset +CWARNS=-pedantic -Wall -Wextra -Werror CFLAGS=-nostdlib -ffreestanding -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 CINCLUDES=-I./kaleid CDEFINES= -CC=$(CCNAME) $(CWARNS) $(CFLAGS) $(CDEFINES) $(CINCLUDES) +CC=$(CCNAME) $(COPTIM) $(CWARNS) $(CFLAGS) $(CDEFINES) $(CINCLUDES) KCC=$(CC) -D_KALEID_KERNEL ASM=nasm @@ -58,20 +59,24 @@ testing: bootloader pseudo_kern COBJDIR=$(OBJDIR)/$(COMMDIR) LOBJDIR=$(OBJDIR)/$(LINXDIR) -COMMDEPS=$(COMMDIR)/common.h $(COMMDIR)/assert.h $(COMMDIR)/atomic.h $(COMMDIR)/config.h \ +COMMDEPS=$(COMMDIR)/common.h $(COMMDIR)/assert.h $(COMMDIR)/atomic.h $(KERNDIR)/config.h \ $(COMMDIR)/status.h -COMMOBJS=$(COBJDIR)/lib/string.o $(COBJDIR)/lib/status.o +COMMOBJS=$(COBJDIR)/lib/string.o $(COBJDIR)/lib/status.o $(COBJDIR)/lib/convert.o $(COBJDIR)/lib/memory.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 + $(KCC) -c $(COMMDIR)/lib/memory.c -o $(COBJDIR)/lib/memory.o + $(KCC) -c $(COMMDIR)/lib/convert.c -o $(COBJDIR)/lib/convert.o -CCC=$(CC2NAME) $(CWARNS) $(CDEFINES) $(CINCLUDES) +CCC=$(CC2NAME) $(COPTIM) $(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 $(COMMDIR)/lib/memory.c -o $(COBJDIR)/lib/memory.o + $(CCC) -c $(COMMDIR)/lib/convert.c -o $(COBJDIR)/lib/convert.o $(CCC) -c $(LINXDIR)/test-common.c -o $(LOBJDIR)/test-common.o $(CCC) $(COMMOBJS) $(LOBJDIR)/test-common.o -o $(BINDIR)/kaleid-common.elf diff --git a/src/kaleid/common/lib/convert.c b/src/kaleid/common/lib/convert.c index 0f76565..284f61f 100644 --- a/src/kaleid/common/lib/convert.c +++ b/src/kaleid/common/lib/convert.c @@ -7,7 +7,7 @@ // Desc: Conversion utilities // //----------------------------------------------------------------------------// -#include "common/convert.h" +#include "common/string.h" // // Digits table for bases <=36 diff --git a/src/kaleid/common/lib/memory.c b/src/kaleid/common/lib/memory.c index 3ebe6ee..547ab79 100644 --- a/src/kaleid/common/lib/memory.c +++ b/src/kaleid/common/lib/memory.c @@ -9,3 +9,71 @@ #include "common/memory.h" +// to be moved +#define QWORD_SIZE 8 +#define QWORD_ALIGN 8 + +// +// Set "qwords"-many aligned qwords starting from ptr to val +// +static inline void *memsetq(void *ptr, ullong uval, size_t qwords) +{ + size_t n; + ullong *uptr = (ullong *)ptr; + + // aligned memory write + for (n = 0; n < qwords; n++) { + *(uptr + n) = uval; + } + + return ptr; +} + +// +// Set "bytes"-many bytes starting from ptr to val +// This is most certainely overkill, but I enjoyed doing this +// Alignment stuff barely matters on modern processors +// This may actually be slower than the naive way +// +void *memset(void *ptr, int val, size_t bytes) +{ + uchar *uptr = (uchar *)ptr; + const size_t qwords = bytes/QWORD_SIZE; + + // get rid of everything after the first byte + val = val & 0xFF; + + // deal with bytes before start of the first aligned qword + while (((ullong)uptr % QWORD_ALIGN) > 0 && bytes--) { + *uptr++ = (uchar)val; + } + + // move qword by qword + if (qwords) { + const ullong uval = ((ullong)val << 56) | ((ullong)val << 48) + | ((ullong)val << 40) | ((ullong)val << 32) + | ((ullong)val << 24) | ((ullong)val << 16) + | ((ullong)val << 8) | ((ullong)val); + + memsetq(uptr, uval, qwords); + + uptr += qwords * QWORD_SIZE; + bytes %= QWORD_SIZE; + } + + // deal with what's left + while (bytes--) { + *uptr++ = (uchar)val; + } + + return ptr; +} + +// +// Set "bytes"-many bytes starting from ptr to 0 +// +void *memzero(void *ptr, size_t bytes) +{ + return memset(ptr, 0, bytes); +} + diff --git a/src/kaleid/common/memory.h b/src/kaleid/common/memory.h index a628141..31f6a6b 100644 --- a/src/kaleid/common/memory.h +++ b/src/kaleid/common/memory.h @@ -14,5 +14,15 @@ # include "common/common.h" #endif +#ifndef _OSK_SOURCE +# define memcpy _osk_memcpy +# define memset _osk_memset +# define memcmp _osk_memcmp +# define memzero _osk_memzero +#endif + +void *memset(void *, int, size_t); +void *memzero(void *, size_t); + #endif diff --git a/src/kaleid/common/types.h b/src/kaleid/common/types.h index 739efa9..bf74580 100644 --- a/src/kaleid/common/types.h +++ b/src/kaleid/common/types.h @@ -15,8 +15,6 @@ # 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; @@ -24,6 +22,11 @@ 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; @@ -33,8 +36,6 @@ typedef ulong pid_t; typedef void *va_list; #endif -typedef short port_t; -typedef short status_t; // XXX limits #endif diff --git a/src/kaleid/linux/test-common.c b/src/kaleid/linux/test-common.c index ee2df6f..bef4749 100644 --- a/src/kaleid/linux/test-common.c +++ b/src/kaleid/linux/test-common.c @@ -9,35 +9,45 @@ #include #include -#include #define KEEP_KALCOMM_TYPES_MINIMAL #include "common/common.h" #include "common/string.h" +#include "common/memory.h" int main(int argc, char *argv[]) { (void)argc; (void)argv; + assert(sizeof(char) == 1); + assert(sizeof(short) == 2); + assert(sizeof(int) == 4); + assert(sizeof(long) == 8); + assert(sizeof(long long) == 8); + + // 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"; - char *test2 = malloc(strlen(test1)); - char *test3 = malloc(strlen(test1)); + const size_t size1 = strlen(test1); + char *test2 = malloc(size1); + char *test3 = malloc(size1); + + memset(test2, 'x', size1); + + size_t it; + for (it = 0; it < size1; it++) { + char c = *(test2 + it); + printf("%c", (c ? c : '-')); + } - printf("1\n"); + puts(""); - #undef strlen - assert(strlen("test string") == _osk_strlen("test string")); - - #undef strcpy - assert(strcmp(strcpy(test2, test1), _osk_strcpy(test3, test1)) == 0); - - // XXX test itoa() and v?sn?printf() - - // tests done - printf("2\n"); - free(test2); free(test3);