From 11686aa86bc0adae2555aa7796fe229770bb02f7 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Thu, 6 Oct 2011 22:16:43 +0800 Subject: [PATCH] Reworking usermode/libc to closer C compliance --- Usermode/Applications/CLIShell_src/header.h | 2 -- Usermode/Applications/CLIShell_src/lib.c | 1 + Usermode/Applications/Makefile.cfg | 2 +- Usermode/Applications/init_src/main.c | 1 - Usermode/Libraries/Makefile.cfg | 2 +- Usermode/Libraries/ld-acess.so_src/common.h | 3 +-- Usermode/Libraries/ld-acess.so_src/elf.c | 2 +- Usermode/Libraries/ld-acess.so_src/lib.c | 29 +++++++++++++++++++++ Usermode/Libraries/ld-acess.so_src/main.c | 1 + Usermode/Libraries/libc.so_src/Makefile | 2 +- Usermode/Libraries/libc.so_src/fileIO.c | 4 +-- Usermode/include/stddef.h | 21 --------------- Usermode/include/stdint.h | 16 +++++------- Usermode/include/stdio.h | 8 +++--- Usermode/include/stdlib.h | 24 ++++++++--------- Usermode/include/string.h | 4 +-- Usermode/include/sys/stat.h | 5 ++-- Usermode/include/sys/types.h | 22 +++++----------- 18 files changed, 71 insertions(+), 78 deletions(-) delete mode 100644 Usermode/include/stddef.h diff --git a/Usermode/Applications/CLIShell_src/header.h b/Usermode/Applications/CLIShell_src/header.h index f90e6518..2733d841 100644 --- a/Usermode/Applications/CLIShell_src/header.h +++ b/Usermode/Applications/CLIShell_src/header.h @@ -5,8 +5,6 @@ #ifndef _HEADER_H #define _HEADER_H -#define NULL ((void*)0) - #define Print(str) do{char*s=(str);write(_stdout,strlen(s)+1,s);}while(0) extern int _stdout; diff --git a/Usermode/Applications/CLIShell_src/lib.c b/Usermode/Applications/CLIShell_src/lib.c index b807dd6e..cfd52e5b 100644 --- a/Usermode/Applications/CLIShell_src/lib.c +++ b/Usermode/Applications/CLIShell_src/lib.c @@ -4,6 +4,7 @@ * See file COPYING for */ #include "header.h" +#include // === CODE === /** diff --git a/Usermode/Applications/Makefile.cfg b/Usermode/Applications/Makefile.cfg index b67e8bd2..b2f36e41 100644 --- a/Usermode/Applications/Makefile.cfg +++ b/Usermode/Applications/Makefile.cfg @@ -5,7 +5,7 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../Makefile.cfg ASFLAGS = -felf -CPPFLAGS = -I$(ACESSUSERDIR)/include/ +CPPFLAGS = -I$(ACESSUSERDIR)/include/ -DARCHDIR_is_$(ARCHDIR) CFLAGS = -fno-stack-protector $(CPPFLAGS) LDFLAGS = -T $(OUTPUTDIR)Libs/acess.ld -rpath-link $(OUTPUTDIR)Libs -L $(OUTPUTDIR)Libs -I /Acess/Libs/ld-acess.so -lld-acess -lc diff --git a/Usermode/Applications/init_src/main.c b/Usermode/Applications/init_src/main.c index a88af9f9..4d66654c 100644 --- a/Usermode/Applications/init_src/main.c +++ b/Usermode/Applications/init_src/main.c @@ -8,7 +8,6 @@ //#include "common.h" // === CONSTANTS === -#define NULL ((void*)0) #define NUM_TERMS 4 #define DEFAULT_TERMINAL "/Devices/VTerm/0" #define DEFAULT_SHELL "/Acess/SBin/login" diff --git a/Usermode/Libraries/Makefile.cfg b/Usermode/Libraries/Makefile.cfg index 9192194c..f9e3d672 100644 --- a/Usermode/Libraries/Makefile.cfg +++ b/Usermode/Libraries/Makefile.cfg @@ -6,6 +6,6 @@ MAKEDEP = $(CC) -M ASFLAGS += -D ARCHDIR=$(ARCHDIR) -CPPFLAGS := -I$(ACESSDIR)/Usermode/include/ -D ARCHDIR=$(ARCHDIR) -D ARCHDIR_IS_$(ARCHDIR)=1 +CPPFLAGS := -I$(ACESSDIR)/Usermode/include/ -DARCHDIR=$(ARCHDIR) -DARCHDIR_is_$(ARCHDIR)=1 CFLAGS := -g -Wall -fPIC -fno-builtin -fno-stack-protector $(CPPFLAGS) LDFLAGS := -g -nostdlib -shared -I/Acess/Libs/ld-acess.so -e SoMain -x -L$(OUTPUTDIR)Libs/ diff --git a/Usermode/Libraries/ld-acess.so_src/common.h b/Usermode/Libraries/ld-acess.so_src/common.h index 941baf37..8fa626a7 100644 --- a/Usermode/Libraries/ld-acess.so_src/common.h +++ b/Usermode/Libraries/ld-acess.so_src/common.h @@ -7,8 +7,7 @@ #ifndef _COMMON_H #define _COMMON_H -#define NULL ((void*)0) - +#include #include // HACK: Replace with underscored diff --git a/Usermode/Libraries/ld-acess.so_src/elf.c b/Usermode/Libraries/ld-acess.so_src/elf.c index 75791d6e..00f2431d 100644 --- a/Usermode/Libraries/ld-acess.so_src/elf.c +++ b/Usermode/Libraries/ld-acess.so_src/elf.c @@ -83,7 +83,7 @@ void *Elf64Relocate(void *Base, char **envp, const char *Filename) for( i = 0; i < hdr->e_phnum; i ++ ) { if(phtab[i].p_type == PT_DYNAMIC) - dyntab = (void *)phtab[i].p_vaddr; + dyntab = (void *)(intptr_t)phtab[i].p_vaddr; if(phtab[i].p_type == PT_LOAD && compiledBase > phtab[i].p_vaddr) compiledBase = phtab[i].p_vaddr; } diff --git a/Usermode/Libraries/ld-acess.so_src/lib.c b/Usermode/Libraries/ld-acess.so_src/lib.c index 27d6ff29..150ae77c 100644 --- a/Usermode/Libraries/ld-acess.so_src/lib.c +++ b/Usermode/Libraries/ld-acess.so_src/lib.c @@ -4,6 +4,7 @@ By thePowersGang */ #include "common.h" +#include // === CODE === char *strcpy(char *dest, const char *src) @@ -70,3 +71,31 @@ int file_exists(const char *filename) close(fd); return 1; } + +uint64_t __udivdi3(uint64_t Num, uint64_t Den) +{ + uint64_t ret = 0, add = 1; + + // Find what power of two times Den is > Num + while( Num >= Den ) + { + Den <<= 1; + add <<= 1; + } + + // Search backwards + while( add > 1 ) + { + add >>= 1; + Den >>= 1; + // If the numerator is > Den, subtract and add to return value + if( Num > Den ) + { + ret += add; + Num -= Den; + } + } +// if(Rem) *Rem = Num; + return ret; +} + diff --git a/Usermode/Libraries/ld-acess.so_src/main.c b/Usermode/Libraries/ld-acess.so_src/main.c index ccdf6b42..0eb28d14 100644 --- a/Usermode/Libraries/ld-acess.so_src/main.c +++ b/Usermode/Libraries/ld-acess.so_src/main.c @@ -3,6 +3,7 @@ By thePowersGang */ #include +#include #include "common.h" // === PROTOTYPES === diff --git a/Usermode/Libraries/libc.so_src/Makefile b/Usermode/Libraries/libc.so_src/Makefile index 2d6801d6..29863015 100644 --- a/Usermode/Libraries/libc.so_src/Makefile +++ b/Usermode/Libraries/libc.so_src/Makefile @@ -8,7 +8,7 @@ CFLAGS += ASFLAGS += LDFLAGS += -soname libc.so -Map map.txt -lgcc -OBJ = stub.o heap.o stdlib.o env.o fileIO.o string.o +OBJ = stub.o heap.o stdlib.o env.o fileIO.o string.o select.o DEPFILES := $(OBJ:%.o=%.d) # signals.o BIN = libc.so diff --git a/Usermode/Libraries/libc.so_src/fileIO.c b/Usermode/Libraries/libc.so_src/fileIO.c index 60def892..5b479aaa 100644 --- a/Usermode/Libraries/libc.so_src/fileIO.c +++ b/Usermode/Libraries/libc.so_src/fileIO.c @@ -19,7 +19,7 @@ #define _stdout 1 // === PROTOTYPES === -EXPORT void itoa(char *buf, uint64_t num, uint base, int minLength, char pad, int bSigned); +EXPORT void itoa(char *buf, uint64_t num, size_t base, int minLength, char pad, int bSigned); struct sFILE *get_file_struct(); // === GLOBALS === @@ -131,7 +131,7 @@ EXPORT void fflush(FILE *fp) ///\todo Implement } -EXPORT long int ftell(FILE *fp) +EXPORT off_t ftell(FILE *fp) { if(!fp || !fp->FD) return -1; diff --git a/Usermode/include/stddef.h b/Usermode/include/stddef.h deleted file mode 100644 index 01636359..00000000 --- a/Usermode/include/stddef.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * AcessOS LibC - * stddef.h - */ -#ifndef _STDDEF_H -#define _STDDEF_H - -// === CONSTANTS === -#ifndef NULL -# define NULL ((void*)0) -#endif - -// === TYPES === -#ifndef size_t -typedef unsigned int size_t; -#endif - -// === MACROS === -#define offsetof(st, m) ((size_t)((uintptr_t)((char *)&((st *)(0))->m - (char *)0 ))) - -#endif diff --git a/Usermode/include/stdint.h b/Usermode/include/stdint.h index 6238a63a..f2707aa4 100644 --- a/Usermode/include/stdint.h +++ b/Usermode/include/stdint.h @@ -1,9 +1,8 @@ /* */ -#ifndef _STDTYPES_H_ -#define _STDTYPES_H_ +#ifndef _STDINT_H_ +#define _STDINT_H_ -//typedef unsigned int uint; typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef unsigned int uint32_t; @@ -14,16 +13,13 @@ typedef signed short int16_t; typedef signed int int32_t; typedef signed long long int64_t; -#ifdef __LP64__ +#if ARCHDIR_is_x86 +typedef uint32_t intptr_t; +typedef uint32_t uintptr_t; +#elif ARCHDIR_is_x86_64 typedef uint64_t intptr_t; typedef uint64_t uintptr_t; -typedef int64_t ptrdiff_t; #else -typedef uint32_t intptr_t; -typedef uint32_t uintptr_t; -typedef int32_t ptrdiff_t; -#endif -#if 0 # error "Unknown pointer size" #endif diff --git a/Usermode/include/stdio.h b/Usermode/include/stdio.h index 552cd4f9..8d950656 100644 --- a/Usermode/include/stdio.h +++ b/Usermode/include/stdio.h @@ -8,13 +8,13 @@ #include #include -// === Types === +/* === Types === */ typedef struct sFILE FILE; -// === CONSTANTS === +/* === CONSTANTS === */ #define EOF (-1) -// --- Standard IO --- +/* --- Standard IO --- */ extern int printf(const char *format, ...); extern int vsnprintf(char *buf, size_t __maxlen, const char *format, va_list args); extern int vsprintf(char *buf, const char *format, va_list args); @@ -26,7 +26,7 @@ extern FILE *freopen(const char *file, const char *mode, FILE *fp); extern FILE *fdopen(int fd, const char *modes); extern int fclose(FILE *fp); extern void fflush(FILE *fp); -extern long int ftell(FILE *fp); +extern off_t ftell(FILE *fp); extern int fseek(FILE *fp, long int amt, int whence); extern size_t fread(void *buf, size_t size, size_t n, FILE *fp); diff --git a/Usermode/include/stdlib.h b/Usermode/include/stdlib.h index 6ea8db50..160993d1 100644 --- a/Usermode/include/stdlib.h +++ b/Usermode/include/stdlib.h @@ -12,27 +12,27 @@ #define EXIT_FAILURE 1 #define EXIT_SUCCESS 0 -// --- Spinlock Macros --- +/* --- Spinlock Macros --- */ +/* TODO: Support non-x86 architectures */ #define DEFLOCK(_name) static int _spinlock_##_name=0; -//#define LOCK(_name) __asm__ __volatile__("jmp ._tst;\n\t._lp:call yield;\n\t._tst:lock btsl $1,(%0);\n\tjc ._lp"::"D"(&_spinlock_##_name)) -#define LOCK(_name) do{int v=1;while(v){__asm__ __volatile__("lock cmpxchgl %%eax, (%1)":"=a"(v):"D"((&_spinlock_##_name)),"a"(1));yield();}}while(0) +#define LOCK(_name) do{int v=1;while(v){__asm__ __volatile__("lock cmpxchgl %0, (%1)":"=a"(v):"D"((&_spinlock_##_name)),"a"(1));yield();}}while(0) #define UNLOCK(_name) __asm__ __volatile__("lock andl $0, (%0)"::"D"(&_spinlock_##_name)) -// --- StdLib --- -extern void _exit(int code) __attribute__((noreturn)); //NOTE: Also defined in acess/sys.h +/* --- StdLib --- */ +extern void _exit(int code) __attribute__((noreturn)); /* NOTE: Also defined in acess/sys.h */ extern int atoi(const char *ptr); extern void exit(int status) __attribute__((noreturn)); extern void atexit(void (*__func)(void)); -extern void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); +extern void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); -// --- Environment --- +/* --- Environment --- */ extern char *getenv(const char *name); -// --- Heap --- -extern void free(void *mem); -extern void *malloc(size_t bytes); -extern void *calloc(size_t __nmemb, size_t __size); -extern void *realloc(void *__ptr, size_t __size); +/* --- Heap --- */ +extern void free(void *mem); +extern void *malloc(size_t bytes); +extern void *calloc(size_t __nmemb, size_t __size); +extern void *realloc(void *__ptr, size_t __size); extern int IsHeap(void *ptr); #ifndef SEEK_CUR diff --git a/Usermode/include/string.h b/Usermode/include/string.h index 6c5a2c6c..b77d8074 100644 --- a/Usermode/include/string.h +++ b/Usermode/include/string.h @@ -7,7 +7,7 @@ #include -// Strings +/* Strings */ extern size_t strlen(const char *string); extern size_t strnlen(const char *string, size_t maxlen); extern int strcmp(const char *str1, const char *str2); @@ -25,7 +25,7 @@ extern char *strstr(const char *str1, const char *str2); extern size_t strcspn(const char *haystack, const char *reject); extern size_t strspn(const char *haystack, const char *accept); -// Memory +/* Memory */ extern void *memset(void *dest, int val, size_t count); extern void *memcpy(void *dest, const void *src, size_t count); extern void *memmove(void *dest, const void *src, size_t count); diff --git a/Usermode/include/sys/stat.h b/Usermode/include/sys/stat.h index e9b9a8fa..10384bae 100644 --- a/Usermode/include/sys/stat.h +++ b/Usermode/include/sys/stat.h @@ -5,9 +5,10 @@ #ifndef _SYS_STAT_H_ #define _SYS_STAT_H_ -#include //EVIL! +#include /* Evil */ +#include -typedef uintptr_t dev_t; // TODO: How to identify a device with Acess +typedef void *dev_t; /* TODO: How to identify a device with Acess */ typedef uint64_t ino_t; typedef unsigned int blksize_t; typedef uint64_t blkcnt_t; diff --git a/Usermode/include/sys/types.h b/Usermode/include/sys/types.h index acf6b7e5..edcaa224 100644 --- a/Usermode/include/sys/types.h +++ b/Usermode/include/sys/types.h @@ -2,7 +2,6 @@ */ #ifndef _SYS_TYPES_H #define _SYS_TYPES_H -//#include typedef struct stat t_fstat; @@ -28,8 +27,8 @@ typedef struct } fd_set; struct s_sysACL { - unsigned long object; //!< Group or user (bit 31 determines) - unsigned long perms; //!< Inverted by bit 31 + unsigned long object; /*!< Group or user (bit 31 determines) */ + unsigned long perms; /*!< Inverted by bit 31 */ }; struct s_sysFInfo { unsigned int mount; @@ -47,19 +46,10 @@ struct s_sysFInfo { typedef struct s_sysFInfo t_sysFInfo; typedef struct s_sysACL t_sysACL; -static inline void FD_ZERO(fd_set *fdsetp) {int i=FD_SETSIZE/16;while(i--)fdsetp->flags[i]=0; } -static inline void FD_CLR(int fd, fd_set *fdsetp) { - if(fd < 0 || fd > FD_SETSIZE) return; - fdsetp->flags[fd/16] &= (fd_set_ent_t) ((~1 << (fd%16))) & 0xFFFF; -} -static inline void FD_SET(int fd, fd_set *fdsetp) { - if(fd < 0 || fd > FD_SETSIZE) return; - fdsetp->flags[fd/16] |= (fd_set_ent_t) (1 << (fd%16)); -} -static inline int FD_ISSET(int fd, fd_set *fdsetp) { - if(fd < 0 || fd > FD_SETSIZE) return 0; - return !!( fdsetp->flags[fd/16] & (1<<(fd%16)) ); -} +extern void FD_ZERO(fd_set *fdsetp); +extern void FD_CLR(int fd, fd_set *fdsetp); +extern void FD_SET(int fd, fd_set *fdsetp); +extern int FD_ISSET(int fd, fd_set *fdsetp); #include -- 2.20.1