From 7c0d1cae67d47b738bb05c0f6fe3b7b215130c2e Mon Sep 17 00:00:00 2001 From: Ali Mashtizadeh Date: Tue, 14 Oct 2014 12:39:26 -0700 Subject: [PATCH] Posix compat work --- include/assert.h | 21 +++++++++++++++++++++ include/stdint.h | 18 +----------------- include/stdio.h | 6 ++++++ include/stdlib.h | 1 + include/time.h | 6 ++++++ lib/libc/SConscript | 2 ++ lib/libc/assert.c | 11 +++++++++++ lib/libc/posix/mman.c | 40 ++++++++++++++++++++++++++++++++++++++++ sys/SConscript | 1 + sys/include/mman.h | 23 +++++++++++++++++++++++ sys/include/types.h | 23 +++++++++++++++++++++++ 11 files changed, 135 insertions(+), 17 deletions(-) create mode 100644 include/assert.h create mode 100644 include/stdio.h create mode 100644 include/time.h create mode 100644 lib/libc/assert.c create mode 100644 lib/libc/posix/mman.c create mode 100644 sys/include/mman.h create mode 100644 sys/include/types.h diff --git a/include/assert.h b/include/assert.h new file mode 100644 index 0000000..8146eae --- /dev/null +++ b/include/assert.h @@ -0,0 +1,21 @@ + +#ifndef __ASSERT_H__ +#define __ASSERT_H__ + +#ifndef NDEBUG + +#define assert(_expr) + +#else + +#define assert(_expr) \ + if (e) { \ + __assert(__func__, __FILE__, __LINE__, #e); \ + } + +#endif + +void __assert(const char *func, const char *file, int line, const char *expr); + +#endif /* __ASSERT_H__ */ + diff --git a/include/stdint.h b/include/stdint.h index 012d8a2..ca6bfe8 100644 --- a/include/stdint.h +++ b/include/stdint.h @@ -2,23 +2,7 @@ #ifndef _STDINT_H_ #define _STDINT_H_ -typedef signed char int8_t; -typedef signed short int16_t; -typedef signed int int32_t; -typedef signed long long int64_t; - -typedef unsigned char uint8_t; -typedef unsigned short uint16_t; -typedef unsigned int uint32_t; -typedef unsigned long long uint64_t; - -typedef int64_t intptr_t; -typedef uint64_t uintptr_t; - -typedef uint64_t size_t; -typedef int64_t ssize_t; - -typedef int64_t off_t; +#include #endif /* _STDINT_H_ */ diff --git a/include/stdio.h b/include/stdio.h new file mode 100644 index 0000000..89deb0f --- /dev/null +++ b/include/stdio.h @@ -0,0 +1,6 @@ + +#ifndef __STDIO_H__ +#define __STDIO_H__ + +#endif /* __STDIO_H__ */ + diff --git a/include/stdlib.h b/include/stdlib.h index fb9f50c..07ab9cd 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -8,6 +8,7 @@ int atexit(void (*function)(void)); void exit(int status); +void abort(void); #endif /* __STDLIB_H__ */ diff --git a/include/time.h b/include/time.h new file mode 100644 index 0000000..25f6c94 --- /dev/null +++ b/include/time.h @@ -0,0 +1,6 @@ + +#ifndef __TIME_H__ +#define __TIME_H__ + +#endif /* __TIME_H__ */ + diff --git a/lib/libc/SConscript b/lib/libc/SConscript index 12b90ca..f934b53 100644 --- a/lib/libc/SConscript +++ b/lib/libc/SConscript @@ -7,8 +7,10 @@ libc_env = env.Clone() src = [ ] src_common = [ + "assert.c", "exit.c", "syscall.c", + "posix/mman.c", ] src_amd64 = [ diff --git a/lib/libc/assert.c b/lib/libc/assert.c new file mode 100644 index 0000000..01d27a8 --- /dev/null +++ b/lib/libc/assert.c @@ -0,0 +1,11 @@ + +#include +#include + +void +__assert(const char *func, const char *file, int line, const char *expr) +{ + //fprintf(stderr, "Assert (%s): %s %s:%d\n", expr, func, file, line); + abort(); +} + diff --git a/lib/libc/posix/mman.c b/lib/libc/posix/mman.c new file mode 100644 index 0000000..57c0b33 --- /dev/null +++ b/lib/libc/posix/mman.c @@ -0,0 +1,40 @@ + +#include +#include + +#if defined(__x86_64__) +#include +#include +#else +#error "Unsupported Architecture!" +#endif + +int +getpagesizes(size_t *pagesize, int nelem) +{ +#if defined(__x86_64__) + if (pagesize) { + if (nelem >= 1) + pagesize[0] = PGSIZE; + if (nelem >= 2) + pagesize[1] = LARGE_PGSIZE; + if (nelem >= 3) + pagesize[2] = HUGE_PGSIZE; + return (nelem > 3 ? 3 : nelem); + } + return 3; +#else +#error "Unsupported Architecture!" +#endif +} + +void* +mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset) +{ +} + +int +munmap(void *addr, size_t len) +{ +} + diff --git a/sys/SConscript b/sys/SConscript index f32df9e..97d9c7f 100644 --- a/sys/SConscript +++ b/sys/SConscript @@ -62,6 +62,7 @@ if (env["ARCH"] == "amd64"): src.append(src_common) kern_env.Append(LINKFLAGS = ['-T', ldscript[1:], '-N', '-nostdlib']) +kern_env.Append(CPPFLAGS = ['-D_KERNEL']) kern_env.Append(CPPFLAGS = ['-ffreestanding', '-fno-builtin', '-nostdinc', '-mno-red-zone', '-mno-mmx', '-mno-sse', '-mcmodel=large']) diff --git a/sys/include/mman.h b/sys/include/mman.h new file mode 100644 index 0000000..ad94a49 --- /dev/null +++ b/sys/include/mman.h @@ -0,0 +1,23 @@ + +#ifndef __SYS_MMAN_H__ +#define __SYS_MMAN_H__ + +#define PROT_NONE 0x00 +#define PROT_READ 0x01 +#define PROT_WRITE 0x02 +#define PROT_EXEC 0x04 + +#define MAP_FILE 0x0010 +#define MAP_ANON 0x0020 +#define MAP_FIXED 0x0040 + + +#ifdef _KERNEL +#else /* _KERNEL */ +int getpagesizes(size_t *pagesize, int nlem); +void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset); +int munmap(void *addr, size_t len); +#endif /* _KERNEL */ + +#endif /* __SYS_MMAN_H__ */ + diff --git a/sys/include/types.h b/sys/include/types.h new file mode 100644 index 0000000..d7b226a --- /dev/null +++ b/sys/include/types.h @@ -0,0 +1,23 @@ + +#ifndef __SYS_TYPES_H__ +#define __SYS_TYPES_H__ + +typedef signed char int8_t; +typedef signed short int16_t; +typedef signed int int32_t; +typedef signed long long int64_t; + +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned int uint32_t; +typedef unsigned long long uint64_t; + +typedef int64_t intptr_t; +typedef uint64_t uintptr_t; +typedef uint64_t size_t; +typedef int64_t ssize_t; + +typedef int64_t off_t; + +#endif /* __SYS_TYPES_H__ */ +