diff options
author | Marvin Borner | 2021-03-26 21:55:50 +0100 |
---|---|---|
committer | Marvin Borner | 2021-03-26 22:02:20 +0100 |
commit | 05498860e8f7b1e8bb27880bc7526de026694804 (patch) | |
tree | 3bddf16e9439a950a3810d45e42a5cefdbcb7663 /libs/libc/inc | |
parent | a96e9c4c858d47f61b89d879aa0ce6a02bdacb38 (diff) |
Renamed libs
Cleaner and more flexible.
Diffstat (limited to 'libs/libc/inc')
-rw-r--r-- | libs/libc/inc/arg.h | 11 | ||||
-rw-r--r-- | libs/libc/inc/assert.h | 28 | ||||
-rw-r--r-- | libs/libc/inc/conv.h | 15 | ||||
-rw-r--r-- | libs/libc/inc/cpu.h | 110 | ||||
-rw-r--r-- | libs/libc/inc/crypto.h | 11 | ||||
-rw-r--r-- | libs/libc/inc/def.h | 59 | ||||
-rw-r--r-- | libs/libc/inc/errno.h | 52 | ||||
-rw-r--r-- | libs/libc/inc/input.h | 279 | ||||
-rw-r--r-- | libs/libc/inc/ioctl.h | 11 | ||||
-rw-r--r-- | libs/libc/inc/list.h | 29 | ||||
-rw-r--r-- | libs/libc/inc/math.h | 8 | ||||
-rw-r--r-- | libs/libc/inc/mem.h | 29 | ||||
-rw-r--r-- | libs/libc/inc/print.h | 27 | ||||
-rw-r--r-- | libs/libc/inc/random.h | 14 | ||||
-rw-r--r-- | libs/libc/inc/socket.h | 38 | ||||
-rw-r--r-- | libs/libc/inc/stack.h | 28 | ||||
-rw-r--r-- | libs/libc/inc/str.h | 22 | ||||
-rw-r--r-- | libs/libc/inc/sys.h | 130 | ||||
-rw-r--r-- | libs/libc/inc/vec.h | 36 |
19 files changed, 937 insertions, 0 deletions
diff --git a/libs/libc/inc/arg.h b/libs/libc/inc/arg.h new file mode 100644 index 0000000..73e592d --- /dev/null +++ b/libs/libc/inc/arg.h @@ -0,0 +1,11 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef ARG_H +#define ARG_H + +typedef __builtin_va_list va_list; +#define va_start __builtin_va_start +#define va_end __builtin_va_end +#define va_arg __builtin_va_arg + +#endif diff --git a/libs/libc/inc/assert.h b/libs/libc/inc/assert.h new file mode 100644 index 0000000..3656c33 --- /dev/null +++ b/libs/libc/inc/assert.h @@ -0,0 +1,28 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef ASSERT_H +#define ASSERT_H + +#include <print.h> + +#ifdef kernel +#include <proc.h> +#define assert(exp) \ + if (!(exp)) { \ + printf("%s:%d: %s: Kernel assertion '%s' failed\n", __FILE__, __LINE__, __func__, \ + #exp); \ + struct proc *assert_proc = proc_current(); \ + if (assert_proc) \ + proc_exit(assert_proc, 1); \ + else \ + __asm__ volatile("cli\nhlt"); \ + } +#elif defined(userspace) +#define assert(exp) \ + if (!(exp)) \ + err(1, "%s:%d: %s: Assertion '%s' failed\n", __FILE__, __LINE__, __func__, #exp); +#else +#error "No lib target specified. Please use -Dkernel or -Duserspace" +#endif + +#endif diff --git a/libs/libc/inc/conv.h b/libs/libc/inc/conv.h new file mode 100644 index 0000000..adf9003 --- /dev/null +++ b/libs/libc/inc/conv.h @@ -0,0 +1,15 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef CONV_H +#define CONV_H + +#include <def.h> + +int atoi(const char *str); +char *htoa(u32 n); +int htoi(const char *str); +char *itoa(int n); + +char *conv_base(int value, char *result, int base, int is_signed); + +#endif diff --git a/libs/libc/inc/cpu.h b/libs/libc/inc/cpu.h new file mode 100644 index 0000000..d709d86 --- /dev/null +++ b/libs/libc/inc/cpu.h @@ -0,0 +1,110 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef CPU_H +#define CPU_H + +#include <def.h> + +u8 inb(u16 port); +u16 inw(u16 port); +u32 inl(u16 port); +void insl(u16 port, void *addr, int n); + +void outb(u16 port, u8 data); +void outw(u16 port, u16 data); +void outl(u16 port, u32 data); + +static inline void spinlock(int *ptr) +{ + int prev; + do + __asm__ volatile("lock xchgl %0,%1" : "=a"(prev) : "m"(*ptr), "a"(1)); + while (prev); +} + +#ifdef kernel +void cpu_print(void); +void cpu_enable_features(void); +void fpu_restore(void); + +u32 cr0_get(void); +void cr0_set(u32 cr0); +u32 cr3_get(void); +void cr3_set(u32 cr3); +u32 cr4_get(void); +void cr4_set(u32 cr4); + +void cli(void); +void sti(void); +void hlt(void); +void idle(void); +void loop(void); + +enum cpuid_requests { CPUID_VENDOR_STRING, CPUID_FEATURES, CPUID_TLB, CPUID_SERIAL }; +enum cpuid_features { + CPUID_FEAT_ECX_SSE3 = 1u << 0, + CPUID_FEAT_ECX_PCLMUL = 1u << 1, + CPUID_FEAT_ECX_DTES64 = 1u << 2, + CPUID_FEAT_ECX_MONITOR = 1u << 3, + CPUID_FEAT_ECX_DS_CPL = 1u << 4, + CPUID_FEAT_ECX_VMX = 1u << 5, + CPUID_FEAT_ECX_SMX = 1u << 6, + CPUID_FEAT_ECX_EST = 1u << 7, + CPUID_FEAT_ECX_TM2 = 1u << 8, + CPUID_FEAT_ECX_SSSE3 = 1u << 9, + CPUID_FEAT_ECX_CID = 1u << 10, + CPUID_FEAT_ECX_FMA = 1u << 12, + CPUID_FEAT_ECX_CX16 = 1u << 13, + CPUID_FEAT_ECX_ETPRD = 1u << 14, + CPUID_FEAT_ECX_PDCM = 1u << 15, + CPUID_FEAT_ECX_PCIDE = 1u << 17, + CPUID_FEAT_ECX_DCA = 1u << 18, + CPUID_FEAT_ECX_SSE4_1 = 1u << 19, + CPUID_FEAT_ECX_SSE4_2 = 1u << 20, + CPUID_FEAT_ECX_x2APIC = 1u << 21, + CPUID_FEAT_ECX_MOVBE = 1u << 22, + CPUID_FEAT_ECX_POPCNT = 1u << 23, + CPUID_FEAT_ECX_AES = 1u << 25, + CPUID_FEAT_ECX_XSAVE = 1u << 26, + CPUID_FEAT_ECX_OSXSAVE = 1u << 27, + CPUID_FEAT_ECX_AVX = 1u << 28, + CPUID_FEAT_ECX_F16C = 1u << 29, + CPUID_FEAT_ECX_RDRND = 1u << 30, + + CPUID_FEAT_EDX_FPU = 1u << 0, + CPUID_FEAT_EDX_VME = 1u << 1, + CPUID_FEAT_EDX_DE = 1u << 2, + CPUID_FEAT_EDX_PSE = 1u << 3, + CPUID_FEAT_EDX_TSC = 1u << 4, + CPUID_FEAT_EDX_MSR = 1u << 5, + CPUID_FEAT_EDX_PAE = 1u << 6, + CPUID_FEAT_EDX_MCE = 1u << 7, + CPUID_FEAT_EDX_CX8 = 1u << 8, + CPUID_FEAT_EDX_APIC = 1u << 9, + CPUID_FEAT_EDX_SEP = 1u << 11, + CPUID_FEAT_EDX_MTRR = 1u << 12, + CPUID_FEAT_EDX_PGE = 1u << 13, + CPUID_FEAT_EDX_MCA = 1u << 14, + CPUID_FEAT_EDX_CMOV = 1u << 15, + CPUID_FEAT_EDX_PAT = 1u << 16, + CPUID_FEAT_EDX_PSE36 = 1u << 17, + CPUID_FEAT_EDX_PSN = 1u << 18, + CPUID_FEAT_EDX_CLF = 1u << 19, + CPUID_FEAT_EDX_DTES = 1u << 21, + CPUID_FEAT_EDX_ACPI = 1u << 22, + CPUID_FEAT_EDX_MMX = 1u << 23, + CPUID_FEAT_EDX_FXSR = 1u << 24, + CPUID_FEAT_EDX_SSE = 1u << 25, + CPUID_FEAT_EDX_SSE2 = 1u << 26, + CPUID_FEAT_EDX_SS = 1u << 27, + CPUID_FEAT_EDX_HTT = 1u << 28, + CPUID_FEAT_EDX_TM1 = 1u << 29, + CPUID_FEAT_EDX_IA64 = 1u << 30, +}; + +u8 cpu_has_cfeature(enum cpuid_features feature); +u8 cpu_has_dfeature(enum cpuid_features feature); + +#endif + +#endif diff --git a/libs/libc/inc/crypto.h b/libs/libc/inc/crypto.h new file mode 100644 index 0000000..bbe8d7e --- /dev/null +++ b/libs/libc/inc/crypto.h @@ -0,0 +1,11 @@ +// MIT License, Copyright (c) 2021 Marvin Borner + +#ifndef CRYPTO_H +#define CRYPTO_H + +#include <def.h> + +void md5(const void *initial_msg, u32 initial_len, u8 digest[16]); +u32 crc32(u32 crc, const void *buf, u32 size); + +#endif diff --git a/libs/libc/inc/def.h b/libs/libc/inc/def.h new file mode 100644 index 0000000..708ffee --- /dev/null +++ b/libs/libc/inc/def.h @@ -0,0 +1,59 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef DEF_H +#define DEF_H + +/** + * Types + */ + +typedef signed char s8; +typedef unsigned char u8; + +typedef signed short s16; +typedef unsigned short u16; + +typedef signed long s32; +typedef unsigned long u32; + +typedef signed long long s64; +typedef unsigned long long u64; + +/** + * Macros + */ + +#define UNUSED(a) ((void)(a)) + +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) + +#define ABS(a) (((a) < 0) ? (-a) : (a)) + +#define NORETURN __attribute__((noreturn)) +#define NO_SANITIZE __attribute__((no_sanitize("undefined"))) +#define PACKED __attribute__((packed)) +#define ALIGNED(align) __attribute__((aligned(align))) + +#define EOF (-1) +#define NULL ((void *)0) + +#define U8_MAX 255 +#define S8_MAX 127 +#define S8_MIN -128 +#define U16_MAX 65535 +#define S16_MAX 32767 +#define S16_MIN -32768 +#define U32_MAX 4294967295 +#define S32_MAX 2147483647 +#define S32_MIN -2147483648 + +#define LONG_MAX S32_MAX +#define LONG_MIN S32_MIN + +#define MILLION 1000000 +#define BILLION 1000000000 +#define TRILLION 1000000000000 +#define QUADRILLION 1000000000000000 + +#endif diff --git a/libs/libc/inc/errno.h b/libs/libc/inc/errno.h new file mode 100644 index 0000000..e226aba --- /dev/null +++ b/libs/libc/inc/errno.h @@ -0,0 +1,52 @@ +// MIT License, Copyright (c) 2021 Marvin Borner + +#ifndef ERRNO_H +#define ERRNO_H + +#include <def.h> + +typedef s32 res; + +#define EOK 0 /* Success */ +#define EPERM 1 /* Operation not permitted */ +#define ENOENT 2 /* No such file or directory */ +#define ESRCH 3 /* No such process */ +#define EINTR 4 /* Interrupted system call */ +#define EIO 5 /* I/O error */ +#define ENXIO 6 /* No such device or address */ +#define E2BIG 7 /* Argument list too long */ +#define ENOEXEC 8 /* Exec format error */ +#define EBADF 9 /* Bad file number */ +#define ECHILD 10 /* No child processes */ +#define EAGAIN 11 /* Try again */ +#define ENOMEM 12 /* Out of memory */ +#define EACCES 13 /* Permission denied */ +#define EFAULT 14 /* Bad address */ +#define ENOTBLK 15 /* Block device required */ +#define EBUSY 16 /* Device or resource busy */ +#define EEXIST 17 /* File exists */ +#define EXDEV 18 /* Cross-device link */ +#define ENODEV 19 /* No such device */ +#define ENOTDIR 20 /* Not a directory */ +#define EISDIR 21 /* Is a directory */ +#define EINVAL 22 /* Invalid argument */ +#define ENFILE 23 /* File table overflow */ +#define EMFILE 24 /* Too many open files */ +#define ENOTTY 25 /* Not a typewriter */ +#define ETXTBSY 26 /* Text file busy */ +#define EFBIG 27 /* File too large */ +#define ENOSPC 28 /* No space left on device */ +#define ESPIPE 29 /* Illegal seek */ +#define EROFS 30 /* Read-only file system */ +#define EMLINK 31 /* Too many links */ +#define EPIPE 32 /* Broken pipe */ +#define EDOM 33 /* Math argument out of domain of func */ +#define ERANGE 34 /* Math result not representable */ +#define EMAX 35 /* Max errno */ + +#ifdef userspace +#define errno (*__errno()) +extern u32 *__errno(void); +#endif + +#endif diff --git a/libs/libc/inc/input.h b/libs/libc/inc/input.h new file mode 100644 index 0000000..5fc7ed1 --- /dev/null +++ b/libs/libc/inc/input.h @@ -0,0 +1,279 @@ +// GPL-2.0 WITH Linux-syscall-note License +// Copyright (c) 1999-2002 Vojtech Pavlik +// Copyright (c) 2015 Hans de Goede <hdegoede@redhat.com> +// This file is from the linux source and was modified by Marvin Borner + +#ifndef SCANCODE_H +#define SCANCODE_H + +#define KEY_RESERVED 0 +#define KEY_ESC 1 +#define KEY_1 2 +#define KEY_2 3 +#define KEY_3 4 +#define KEY_4 5 +#define KEY_5 6 +#define KEY_6 7 +#define KEY_7 8 +#define KEY_8 9 +#define KEY_9 10 +#define KEY_0 11 +#define KEY_MINUS 12 +#define KEY_EQUAL 13 +#define KEY_BACKSPACE 14 +#define KEY_TAB 15 +#define KEY_Q 16 +#define KEY_W 17 +#define KEY_E 18 +#define KEY_R 19 +#define KEY_T 20 +#define KEY_Y 21 +#define KEY_U 22 +#define KEY_I 23 +#define KEY_O 24 +#define KEY_P 25 +#define KEY_LEFTBRACE 26 +#define KEY_RIGHTBRACE 27 +#define KEY_ENTER 28 +#define KEY_LEFTCTRL 29 +#define KEY_A 30 +#define KEY_S 31 +#define KEY_D 32 +#define KEY_F 33 +#define KEY_G 34 +#define KEY_H 35 +#define KEY_J 36 +#define KEY_K 37 +#define KEY_L 38 +#define KEY_SEMICOLON 39 +#define KEY_APOSTROPHE 40 +#define KEY_GRAVE 41 +#define KEY_LEFTSHIFT 42 +#define KEY_BACKSLASH 43 +#define KEY_Z 44 +#define KEY_X 45 +#define KEY_C 46 +#define KEY_V 47 +#define KEY_B 48 +#define KEY_N 49 +#define KEY_M 50 +#define KEY_COMMA 51 +#define KEY_DOT 52 +#define KEY_SLASH 53 +#define KEY_RIGHTSHIFT 54 +#define KEY_KPASTERISK 55 +#define KEY_LEFTALT 56 +#define KEY_SPACE 57 +#define KEY_CAPSLOCK 58 +#define KEY_F1 59 +#define KEY_F2 60 +#define KEY_F3 61 +#define KEY_F4 62 +#define KEY_F5 63 +#define KEY_F6 64 +#define KEY_F7 65 +#define KEY_F8 66 +#define KEY_F9 67 +#define KEY_F10 68 +#define KEY_NUMLOCK 69 +#define KEY_SCROLLLOCK 70 +#define KEY_KP7 71 +#define KEY_KP8 72 +#define KEY_KP9 73 +#define KEY_KPMINUS 74 +#define KEY_KP4 75 +#define KEY_KP5 76 +#define KEY_KP6 77 +#define KEY_KPPLUS 78 +#define KEY_KP1 79 +#define KEY_KP2 80 +#define KEY_KP3 81 +#define KEY_KP0 82 +#define KEY_KPDOT 83 + +#define KEY_ZENKAKUHANKAKU 85 +#define KEY_102ND 86 +#define KEY_F11 87 +#define KEY_F12 88 +#define KEY_RO 89 +#define KEY_KATAKANA 90 +#define KEY_HIRAGANA 91 +#define KEY_HENKAN 92 +#define KEY_KATAKANAHIRAGANA 93 +#define KEY_MUHENKAN 94 +#define KEY_KPJPCOMMA 95 +#define KEY_KPENTER 96 +#define KEY_RIGHTCTRL 97 +#define KEY_KPSLASH 98 +#define KEY_SYSRQ 99 +#define KEY_RIGHTALT 100 +#define KEY_LINEFEED 101 +#define KEY_HOME 102 +#define KEY_UP 103 +#define KEY_PAGEUP 104 +#define KEY_LEFT 105 +#define KEY_RIGHT 106 +#define KEY_END 107 +#define KEY_DOWN 108 +#define KEY_PAGEDOWN 109 +#define KEY_INSERT 110 +#define KEY_DELETE 111 +#define KEY_MACRO 112 +#define KEY_MUTE 113 +#define KEY_VOLUMEDOWN 114 +#define KEY_VOLUMEUP 115 +#define KEY_POWER 116 +#define KEY_KPEQUAL 117 +#define KEY_KPPLUSMINUS 118 +#define KEY_PAUSE 119 +#define KEY_SCALE 120 + +#define KEY_KPCOMMA 121 +#define KEY_HANGEUL 122 +#define KEY_HANGUEL KEY_HANGEUL +#define KEY_HANJA 123 +#define KEY_YEN 124 +#define KEY_LEFTMETA 125 +#define KEY_RIGHTMETA 126 +#define KEY_COMPOSE 127 + +#define KEY_STOP 128 +#define KEY_AGAIN 129 +#define KEY_PROPS 130 +#define KEY_UNDO 131 +#define KEY_FRONT 132 +#define KEY_COPY 133 +#define KEY_OPEN 134 +#define KEY_PASTE 135 +#define KEY_FIND 136 +#define KEY_CUT 137 +#define KEY_HELP 138 +#define KEY_MENU 139 +#define KEY_CALC 140 +#define KEY_SETUP 141 +#define KEY_SLEEP 142 +#define KEY_WAKEUP 143 +#define KEY_FILE 144 +#define KEY_SENDFILE 145 +#define KEY_DELETEFILE 146 +#define KEY_XFER 147 +#define KEY_PROG1 148 +#define KEY_PROG2 149 +#define KEY_WWW 150 +#define KEY_MSDOS 151 +#define KEY_COFFEE 152 +#define KEY_SCREENLOCK KEY_COFFEE +#define KEY_ROTATE_DISPLAY 153 +#define KEY_DIRECTION KEY_ROTATE_DISPLAY +#define KEY_CYCLEWINDOWS 154 +#define KEY_MAIL 155 +#define KEY_BOOKMARKS 156 +#define KEY_COMPUTER 157 +#define KEY_BACK 158 +#define KEY_FORWARD 159 +#define KEY_CLOSECD 160 +#define KEY_EJECTCD 161 +#define KEY_EJECTCLOSECD 162 +#define KEY_NEXTSONG 163 +#define KEY_PLAYPAUSE 164 +#define KEY_PREVIOUSSONG 165 +#define KEY_STOPCD 166 +#define KEY_RECORD 167 +#define KEY_REWIND 168 +#define KEY_PHONE 169 +#define KEY_ISO 170 +#define KEY_CONFIG 171 +#define KEY_HOMEPAGE 172 +#define KEY_REFRESH 173 +#define KEY_EXIT 174 +#define KEY_MOVE 175 +#define KEY_EDIT 176 +#define KEY_SCROLLUP 177 +#define KEY_SCROLLDOWN 178 +#define KEY_KPLEFTPAREN 179 +#define KEY_KPRIGHTPAREN 180 +#define KEY_NEW 181 +#define KEY_REDO 182 + +#define KEY_F13 183 +#define KEY_F14 184 +#define KEY_F15 185 +#define KEY_F16 186 +#define KEY_F17 187 +#define KEY_F18 188 +#define KEY_F19 189 +#define KEY_F20 190 +#define KEY_F21 191 +#define KEY_F22 192 +#define KEY_F23 193 +#define KEY_F24 194 + +#define KEY_PLAYCD 200 +#define KEY_PAUSECD 201 +#define KEY_PROG3 202 +#define KEY_PROG4 203 +#define KEY_DASHBOARD 204 +#define KEY_SUSPEND 205 +#define KEY_CLOSE 206 +#define KEY_PLAY 207 +#define KEY_FASTFORWARD 208 +#define KEY_BASSBOOST 209 +#define KEY_PRINT 210 +#define KEY_HP 211 +#define KEY_CAMERA 212 +#define KEY_SOUND 213 +#define KEY_QUESTION 214 +#define KEY_EMAIL 215 +#define KEY_CHAT 216 +#define KEY_SEARCH 217 +#define KEY_CONNECT 218 +#define KEY_FINANCE 219 +#define KEY_SPORT 220 +#define KEY_SHOP 221 +#define KEY_ALTERASE 222 +#define KEY_CANCEL 223 +#define KEY_BRIGHTNESSDOWN 224 +#define KEY_BRIGHTNESSUP 225 +#define KEY_MEDIA 226 + +#define KEY_SWITCHVIDEOMODE 227 +#define KEY_KBDILLUMTOGGLE 228 +#define KEY_KBDILLUMDOWN 229 +#define KEY_KBDILLUMUP 230 + +#define KEY_SEND 231 +#define KEY_REPLY 232 +#define KEY_FORWARDMAIL 233 +#define KEY_SAVE 234 +#define KEY_DOCUMENTS 235 + +#define KEY_BATTERY 236 + +#define KEY_BLUETOOTH 237 +#define KEY_WLAN 238 +#define KEY_UWB 239 + +#define KEY_UNKNOWN 240 + +#define KEY_VIDEO_NEXT 241 +#define KEY_VIDEO_PREV 242 +#define KEY_BRIGHTNESS_CYCLE 243 +#define KEY_BRIGHTNESS_AUTO 244 +#define KEY_BRIGHTNESS_ZERO KEY_BRIGHTNESS_AUTO +#define KEY_DISPLAY_OFF 245 + +#define KEY_WWAN 246 +#define KEY_WIMAX KEY_WWAN +#define KEY_RFKILL 247 + +#define KEY_MICMUTE 248 + +#define KEY_NUMERIC(code) ((code) >= KEY_1 && (code) <= KEY_0) +#define KEY_ALPHABETIC(code) \ + (((code) >= KEY_Q && (code) <= KEY_P) || ((code) >= KEY_A && (code) <= KEY_L) || \ + ((code) >= KEY_Z && (code) <= KEY_M)) +#define KEY_ALPHANUMERIC(code) (KEY_NUMERIC((code)) || KEY_ALPHABETIC((code))) + +typedef int fortytwo; + +#endif diff --git a/libs/libc/inc/ioctl.h b/libs/libc/inc/ioctl.h new file mode 100644 index 0000000..c3eec56 --- /dev/null +++ b/libs/libc/inc/ioctl.h @@ -0,0 +1,11 @@ +// MIT License, Copyright (c) 2021 Marvin Borner + +#ifndef IOCTL +#define IOCTL + +// FB interface +#define IO_FB_GET 0 + +int ioctl_is_awesome; // GCC is not + +#endif diff --git a/libs/libc/inc/list.h b/libs/libc/inc/list.h new file mode 100644 index 0000000..0b82b48 --- /dev/null +++ b/libs/libc/inc/list.h @@ -0,0 +1,29 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef LIST_H +#define LIST_H + +#include <def.h> + +struct list { + struct node *head; +}; + +struct node { + void *data; + int nonce; + struct node *next; + struct node *prev; +}; + +struct list *list_new(void); +void list_destroy(struct list *list); +/* struct node *list_new_node(); */ // TODO: Make node-specific things static/private? +/* void list_add_node(struct list *list, struct node *node); */ +struct node *list_add(struct list *list, void *data); +struct list *list_remove(struct list *list, struct node *node); +struct node *list_last(struct list *list); +struct list *list_swap(struct list *list, struct node *a, struct node *b); +struct node *list_first_data(struct list *list, void *data); + +#endif diff --git a/libs/libc/inc/math.h b/libs/libc/inc/math.h new file mode 100644 index 0000000..82f431f --- /dev/null +++ b/libs/libc/inc/math.h @@ -0,0 +1,8 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef MATH_H +#define MATH_H + +int pow(int base, int exp); + +#endif diff --git a/libs/libc/inc/mem.h b/libs/libc/inc/mem.h new file mode 100644 index 0000000..ec00628 --- /dev/null +++ b/libs/libc/inc/mem.h @@ -0,0 +1,29 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef MEM_H +#define MEM_H + +#include <def.h> + +void *malloc_debug(u32 size, const char *file, int line, const char *func, const char *inp); +void free_debug(void *ptr, const char *file, int line, const char *func, const char *inp); +#define malloc(size) malloc_debug((u32)(size), __FILE__, __LINE__, __func__, #size) +#define free(ptr) free_debug((void *)(ptr), __FILE__, __LINE__, __func__, #ptr) +void *realloc(void *ptr, u32 size); +void *zalloc(u32 size); + +#ifdef kernel +#define STACK_START 0x00500000 // Defined it bootloader +#define STACK_SIZE 0x1000 // idk +#elif defined(userspace) +#else +#error "No lib target specified. Please use -Dkernel or -Duserspace" +#endif + +void *memcpy(void *dest, const void *src, u32 n); +void *memset(void *dest, int val, u32 n); +void *memchr(void *src, int c, u32 n); +int memcmp(const void *s1, const void *s2, u32 n); +int mememp(const u8 *buf, u32 n); + +#endif diff --git a/libs/libc/inc/print.h b/libs/libc/inc/print.h new file mode 100644 index 0000000..58b5dc6 --- /dev/null +++ b/libs/libc/inc/print.h @@ -0,0 +1,27 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef PRINT_H +#define PRINT_H + +#include "arg.h" +#include <def.h> + +int printf(const char *format, ...); +int vprintf(const char *format, va_list ap); +int sprintf(char *str, const char *format, ...); +int vsprintf(char *str, const char *format, va_list ap); +int print(const char *str); +NORETURN void panic(const char *format, ...); + +#ifdef userspace +int vfprintf(const char *path, const char *format, va_list ap); +int fprintf(const char *path, const char *format, ...); +int log(const char *format, ...); +int err(int code, const char *format, ...); +#else +#include <proc.h> +int print_app(enum stream_defaults id, const char *proc_name, const char *str); +void print_trace(u32 count); +#endif + +#endif diff --git a/libs/libc/inc/random.h b/libs/libc/inc/random.h new file mode 100644 index 0000000..a82524c --- /dev/null +++ b/libs/libc/inc/random.h @@ -0,0 +1,14 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef RANDOM_H +#define RANDOM_H + +#include <def.h> + +void srand(u32 seed); +u32 rdrand(void); +u32 rdseed(void); +u32 rand(void); +char *randstr(u32 size); + +#endif diff --git a/libs/libc/inc/socket.h b/libs/libc/inc/socket.h new file mode 100644 index 0000000..18bc316 --- /dev/null +++ b/libs/libc/inc/socket.h @@ -0,0 +1,38 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef SOCKET_H +#define SOCKET_H + +#include <def.h> +#include <list.h> + +// TODO: Use actual socket types (stream etc) +enum socket_type { S_TCP, S_UDP }; +enum socket_state { S_CONNECTING, S_CONNECTED, S_OPEN, S_CLOSING, S_CLOSED, S_FAILED }; + +struct tcp_socket { + u32 seq_no; + u32 ack_no; + u32 state; +}; + +struct socket_data { + u8 *data; + u32 length; +}; + +struct socket { + u32 pid; + u32 ip_addr; + u32 dst_port; + u32 src_port; + enum socket_state state; + enum socket_type type; + struct list *packets; + union { + struct tcp_socket tcp; + /* struct udp_socket udp; */ + } prot; +}; + +#endif diff --git a/libs/libc/inc/stack.h b/libs/libc/inc/stack.h new file mode 100644 index 0000000..f5ad52b --- /dev/null +++ b/libs/libc/inc/stack.h @@ -0,0 +1,28 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef STACK_H +#define STACK_H + +#include <def.h> + +struct stack_node { + void *data; + int nonce; + struct stack_node *next; + struct stack_node *prev; +}; + +struct stack { + struct stack_node *tail; +}; + +struct stack *stack_new(void); +void stack_destroy(struct stack *stack); +u32 stack_empty(struct stack *stack); +u32 stack_push_bot(struct stack *stack, void *data); +u32 stack_push(struct stack *stack, void *data); +void *stack_pop(struct stack *stack); +void *stack_peek(struct stack *stack); +void stack_clear(struct stack *stack); + +#endif diff --git a/libs/libc/inc/str.h b/libs/libc/inc/str.h new file mode 100644 index 0000000..d0a521f --- /dev/null +++ b/libs/libc/inc/str.h @@ -0,0 +1,22 @@ +// MIT License, Copyright (c) 2020 Marvin Borner + +#ifndef STR_H +#define STR_H + +#include <def.h> + +u32 strlen(const char *s); +char *strcpy(char *dst, const char *src); +char *strncpy(char *dst, const char *src, u32 n); +char *strchr(char *s, int c); +char *strrchr(char *s, int c); +char *strcat(char *dst, const char *src); +char *strncat(char *dst, const char *src, u32 n); +int strcmp(const char *s1, const char *s2); +int strncmp(const char *s1, const char *s2, u32 n); +char *strinv(char *s); +char *strdup(const char *s); + +const char *strerror(u32 err); + +#endif diff --git a/libs/libc/inc/sys.h b/libs/libc/inc/sys.h new file mode 100644 index 0000000..19fb3ee --- /dev/null +++ b/libs/libc/inc/sys.h @@ -0,0 +1,130 @@ +// MIT License, Copyright (c) 2020 Marvin Borner +// Syscall implementation + +#ifndef SYS_H +#define SYS_H + +#include <def.h> +#include <errno.h> + +#define KEYBOARD_MAGIC 0x555555 +#define MOUSE_MAGIC 0xaaaaaa + +#define SYS_BOOT_MAGIC 0x18122002 +#define SYS_BOOT_REBOOT 0xeeb007 +#define SYS_BOOT_SHUTDOWN 0xdead + +enum sys { + SYS_LOOP, // To infinity and beyond (debug)! + SYS_ALLOC, // Allocate memory + SYS_SHACCESS, // Access shared memory + SYS_FREE, // Free memory + SYS_STAT, // Get file information + SYS_READ, // Read file + SYS_WRITE, // Write to file + SYS_IOCTL, // Interact with a file/device + SYS_POLL, // Wait for multiple files + SYS_EXEC, // Execute path + SYS_EXIT, // Exit current process + SYS_BOOT, // Boot functions (e.g. reboot/shutdown) + SYS_YIELD, // Switch to next process + SYS_TIME, // Get kernel time + /* SYS_NET_OPEN, // Open network socket */ + /* SYS_NET_CLOSE, // Close network socket */ + /* SYS_NET_CONNECT, // Connect to destination */ + /* SYS_NET_SEND, // Send to socket */ + /* SYS_NET_RECEIVE, // Receive data from socket */ +}; + +struct event_keyboard { + u32 magic; + u32 scancode; + u8 press; +}; + +struct event_mouse { + u32 magic; + s32 diff_x; + s32 diff_y; + u8 but1; + u8 but2; + u8 but3; +}; + +struct stat { + u32 dev_id; + u32 mode; + u32 uid; + u32 gid; + u32 size; +}; + +#if defined(userspace) + +/** + * Syscall wrappers + */ + +void loop(void); +void exit(s32 status); +res read(const char *path, void *buf, u32 offset, u32 count); +res write(const char *path, const void *buf, u32 offset, u32 count); +res ioctl(const char *path, ...); +res stat(const char *path, struct stat *buf); +res poll(const char **files); +res exec(const char *path, ...); +res yield(void); +res boot(u32 cmd); +u32 time(void); + +res sys_alloc(u32 size, u32 *addr); +res sys_free(void *ptr); +res shalloc(u32 size, u32 *addr, u32 *id); +res shaccess(u32 id, u32 *addr, u32 *size); + +static inline u32 getpid(void) +{ + static u32 buf = 0; + if (buf) + return buf; + read("/proc/self/pid", &buf, 0, sizeof(buf)); + return buf; +} + +#include <print.h> +#include <str.h> +static inline u32 pidof(const char *name) +{ + u32 curr = 1; + char buf[32] = { 0 }, path[32] = { 0 }; + while (curr < 1000) { // Max pid?? + if (sprintf(path, "/proc/%d/name", curr) > 0 && read(path, buf, 0, 32) > 0) + if (!strcmp(name, buf)) + return curr; + + curr++; + } + + return -1; +} + +// Simple read wrapper +#include <mem.h> +static inline void *sread(const char *path) +{ + struct stat s = { 0 }; + if (stat(path, &s) != 0 || !s.size) + return NULL; + void *buf = malloc(s.size); + read(path, buf, 0, s.size); + return buf; +} + +/** + * At exit + */ + +void atexit(void (*func)(void)); + +#endif +#endif diff --git a/libs/libc/inc/vec.h b/libs/libc/inc/vec.h new file mode 100644 index 0000000..a036931 --- /dev/null +++ b/libs/libc/inc/vec.h @@ -0,0 +1,36 @@ +// MIT License, Copyright (c) 2021 Marvin Borner + +#ifndef VEC_H +#define VEC_H + +#include <def.h> + +typedef struct vec2 { + u32 x, y; +} vec2; + +typedef struct vec3 { + u32 x, y, z; +} vec3; + +#define vec2(x, y) ((vec2){ (x), (y) }) +#define vec2to3(a, z) ((vec3){ a.x, a.y, (z) }) +#define vec2_add(a, b) ((vec2){ a.x + b.x, a.y + b.y }) +#define vec2_sub(a, b) ((vec2){ a.x - b.x, a.y - b.y }) +#define vec2_mul(a, b) ((vec2){ a.x * (b), a.y * (b) }) +#define vec2_div(a, b) ((vec2){ a.x / (b), a.y / (b) }) +#define vec2_dot(a, b) ((u32)(a.x * b.x + a.y * b.y)) +#define vec2_eq(a, b) (a.x == b.x && a.y == b.y) + +#define vec3(x, y, z) ((vec3){ (x), (y), (z) }) +#define vec3to2(a) ((vec2){ a.x, a.y }) +#define vec3_add(a, b) ((vec3){ a.x + b.x, a.y + b.y, a.z + b.z }) +#define vec3_sub(a, b) ((vec3){ a.x - b.x, a.y - b.y, a.z - b.z }) +#define vec3_mul(a, b) ((vec3){ a.x * (b), a.y * (b), a.z * (b) }) +#define vec3_div(a, b) ((vec3){ a.x / (b), a.y / (b), a.z / (b) }) +#define vec3_dot(a, b) ((u32)(a.x * b.x + a.y * b.y + a.z * b.z)) +#define vec3_eq(a, b) (a.x == b.x && a.y == b.y && a.z == c.z) +#define vec3_cross(a, b) \ + ((vec3){ a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x }) + +#endif |