summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Santmyer <jon@jonsantmyer.com>2021-11-23 12:57:54 -0500
committerJon Santmyer <jon@jonsantmyer.com>2021-11-23 12:57:54 -0500
commitcfd5c05a4d13519c3d21390ccd7798838f4bf1b5 (patch)
treeccf36229b5021a295022f626dbce6cbcd3908879
parentd2c79a9aad11acbb34e3c1d88cefa3568f1139e4 (diff)
downloadmodit-slim-cfd5c05a4d13519c3d21390ccd7798838f4bf1b5.tar.gz
modit-slim-cfd5c05a4d13519c3d21390ccd7798838f4bf1b5.tar.bz2
modit-slim-cfd5c05a4d13519c3d21390ccd7798838f4bf1b5.zip
add tester printf; begin work on initrd server
-rw-r--r--app/init/ird.c29
-rw-r--r--app/init/ird.h8
-rw-r--r--app/init/main.c8
m---------kernel0
-rw-r--r--lib/c/ctype/tocase.c12
-rw-r--r--lib/c/stdio/printf.c23
-rw-r--r--lib/c/stdio/vsnprintf.c122
-rw-r--r--lib/c/string/itoa.c7
-rw-r--r--lib/c/string/strcase.c18
-rw-r--r--lib/c/string/strchr.c20
-rw-r--r--lib/modit/mem/mmap.c11
-rw-r--r--root/include/ctype.h7
-rw-r--r--root/include/stdio.h26
-rw-r--r--root/include/string.h9
-rw-r--r--root/include/sys/mem.h (renamed from root/include/sys/alloc.h)1
15 files changed, 294 insertions, 7 deletions
diff --git a/app/init/ird.c b/app/init/ird.c
index e69de29..f2f2a30 100644
--- a/app/init/ird.c
+++ b/app/init/ird.c
@@ -0,0 +1,29 @@
+#include "ird.h"
+#include <sys/mem.h>
+#include <modit/syscall.h>
+#include <modit/task.h>
+#include <stdio.h>
+
+DEFN_SYSCALL(initrd, SYSCALL_INITRD_GET)
+
+struct initrd_file *initrd_head = NULL;
+
+static uintptr_t tar_start;
+static uintptr_t tar_end;
+
+void
+ird_setup(void)
+{
+ uintptr_t irdp = 0;
+ size_t irds = 0;
+
+ struct syscall_initrd_data data = { .phys = &irdp, .len = &irds };
+ _syscall_initrd(&data);
+
+ tar_end = MODIT_USER_STACK - 0x1000000;
+ tar_start = tar_end - irds;
+
+ for(uintptr_t i = 0; i < irds; i += 0x1000) {
+ mmap(irdp + i, tar_start + i);
+ }
+}
diff --git a/app/init/ird.h b/app/init/ird.h
new file mode 100644
index 0000000..aaa072c
--- /dev/null
+++ b/app/init/ird.h
@@ -0,0 +1,8 @@
+#ifndef INIT_IRD_H
+#define INIT_IRD_H 1
+
+#include "modit/initrd.h"
+
+void ird_setup(void);
+
+#endif
diff --git a/app/init/main.c b/app/init/main.c
index a72467a..a091a5a 100644
--- a/app/init/main.c
+++ b/app/init/main.c
@@ -1,6 +1,5 @@
#include "scheduler.h"
#include "rpcsrv.h"
-#include "spawner.h"
#include "sys/cid.h"
#include "sys/ipc.h"
@@ -9,12 +8,6 @@
#include <string.h>
-DEFN_SYSCALL(print, SYSCALL_PRINT);
-void prints(const char *msg) {
- struct syscall_print_data data = { .msg = msg };
- _syscall_print(&data);
-}
-
int
main(int argc, char **argv)
{
@@ -22,6 +15,7 @@ main(int argc, char **argv)
(void)argv;
rpcsrv_setup();
scheduler_setup();
+ ird_setup();
rpc_handle(false);
}
diff --git a/kernel b/kernel
-Subproject 5943a515fdda7b2c148122e779aed16312be7cb
+Subproject 6f895bf2b6609a9b98bf8b74a6f7d140f3ed588
diff --git a/lib/c/ctype/tocase.c b/lib/c/ctype/tocase.c
new file mode 100644
index 0000000..be516f2
--- /dev/null
+++ b/lib/c/ctype/tocase.c
@@ -0,0 +1,12 @@
+#include <ctype.h>
+
+int
+toupper(int c)
+{
+ if(c >= 'a' && c <= 'z') return c - 32;
+}
+int
+tolower(int c)
+{
+ if(c >= 'A' && c <= 'Z') return c + 32;
+}
diff --git a/lib/c/stdio/printf.c b/lib/c/stdio/printf.c
new file mode 100644
index 0000000..7fd796c
--- /dev/null
+++ b/lib/c/stdio/printf.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include "modit/syscall.h"
+
+DEFN_SYSCALL(print, SYSCALL_PRINT);
+void prints(const char *msg) {
+ struct syscall_print_data data = { .msg = msg };
+ _syscall_print(&data);
+}
+
+int
+printf(const char *restrict format, ...)
+{
+ va_list ap;
+ va_start(ap, format);
+
+ char *buffer = malloc(1024);
+ int r = vsnprintf(buffer, 1024, format, ap);
+ prints(buffer);
+
+ free(buffer);
+ va_end(ap);
+ return r;
+}
diff --git a/lib/c/stdio/vsnprintf.c b/lib/c/stdio/vsnprintf.c
new file mode 100644
index 0000000..b5c6a15
--- /dev/null
+++ b/lib/c/stdio/vsnprintf.c
@@ -0,0 +1,122 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+
+static int
+__vsnprintf_itoa(char *term, intmax_t val, int base)
+{
+ int size = 0;
+ intmax_t dec = 1;
+ bool neg = false;
+ if(val < 0) {
+ val = 0 - val;
+ }
+ while(dec <= val + 1){
+ int digit = (val / dec) % base;
+ if(term != NULL){
+ *term = digit > 10 ? (digit - 10) + 'a' : digit + '0';
+ term--;
+ }
+ dec *= base;
+ size++;
+ }
+ return size;
+}
+
+static int
+__vsnprintf_copytill(char *dest, char *src, size_t len, size_t size)
+{
+ int wrlen = len > (size - 1) ? size - 1 : len;
+ memcpy(dest, src, wrlen);
+ return wrlen;
+}
+
+int
+vsnprintf(
+ char *restrict str,
+ size_t size,
+ const char *restrict format,
+ va_list ap)
+{
+ size_t wrsize = 0;
+ bool truncated = false;
+ for(const char *c = format; *c && size > 1; c++, size--) {
+ if(*c != '%') {
+ str[wrsize++] = *c;
+ continue;
+ }
+ c++;
+ //Flags
+ bool alternate = false, zero = false, leftadj = false, spaced = false, sign = false;
+
+ while(strchr("#0- +", *c) != NULL) {
+ switch(*c) {
+ case '#': alternate = true; break;
+ case '0': zero = true; break;
+ case '-': leftadj = true; break;
+ case ' ': spaced = true; break;
+ case '+': sign = true; break;
+ }
+ c++;
+ }
+ //Width
+ int minwidth = 0;
+ if(*c == '*') {
+ minwidth = va_arg(ap, int);
+ c++;
+ }else {
+ while(*c >= '0' && *c <= '9') {
+ minwidth *= 10;
+ minwidth += *c - '0';
+ c++;
+ }
+ }
+
+ //TODO: Precision
+ //TODO: Length Modifier
+ //Conversion
+ int base = 10;
+ bool varsign = true;
+ bool upper = false;
+ switch(*c) {
+ case 'p': //FALLTHROUGH
+ case 'X':
+ upper = true;
+ //FALLTHROUGH
+ case 'x':
+ base = 16;
+ //FALLTHROUGH
+ case 'u':
+ varsign = false;
+ //FALLTHROUGH
+ case 'd':
+ //FALLTHROUGH
+ case 'i': {
+ intmax_t val = va_arg(ap, intmax_t);
+ int pwidth = __vsnprintf_itoa(NULL, val, base);
+ int bufwidth = pwidth > minwidth ? pwidth : minwidth; //Make sure intstr will fit in buffer
+ if(val < 0 || sign) bufwidth++; //Make room for sign
+ char buffer[bufwidth];
+ if(zero) memset(buffer, '0', bufwidth);
+ if(spaced) memset(buffer, ' ', bufwidth);
+ if(val < 0 || sign) //Set sign at top of buffer
+ buffer[0] = val > 0 ? '+' : '-';
+ __vsnprintf_itoa(&buffer[bufwidth - 1], val, base); //Write intstr to buffer starting at end
+ if(upper) strupp(buffer);
+ wrsize += __vsnprintf_copytill(&str[wrsize], buffer, bufwidth, size); //Copy until reaching either bufwidth or size
+ size -= bufwidth;
+ } break;
+ case 's': {
+ const char *val = va_arg(ap, const char*);
+ int vallen = strlen(val) - 1;
+ char buffer[vallen];
+ memcpy(buffer, val, vallen);
+ wrsize += __vsnprintf_copytill(&str[wrsize], buffer, vallen, size);
+ size -= vallen;
+ } break;
+ }
+ }
+ str[wrsize] = 0;
+ return wrsize;
+}
diff --git a/lib/c/string/itoa.c b/lib/c/string/itoa.c
new file mode 100644
index 0000000..4982d9e
--- /dev/null
+++ b/lib/c/string/itoa.c
@@ -0,0 +1,7 @@
+#include <string.h>
+
+char *
+itoa(intmax_t value, char *str, int base)
+{
+
+}
diff --git a/lib/c/string/strcase.c b/lib/c/string/strcase.c
new file mode 100644
index 0000000..addb537
--- /dev/null
+++ b/lib/c/string/strcase.c
@@ -0,0 +1,18 @@
+#include <string.h>
+#include <ctype.h>
+
+char *
+strlow(char *s){
+ for(char *c = s; *c; c++) {
+ *c = tolower(*c);
+ }
+ return s;
+}
+
+char *strupp(char *s)
+{
+ for(char *c = s; *c; c++ ){
+ *c = toupper(*c);
+ }
+ return s;
+}
diff --git a/lib/c/string/strchr.c b/lib/c/string/strchr.c
new file mode 100644
index 0000000..19ee149
--- /dev/null
+++ b/lib/c/string/strchr.c
@@ -0,0 +1,20 @@
+#include <string.h>
+
+char *
+strchr(const char *s, char c)
+{
+ for(; *s; s++) {
+ if(*s == c) return s;
+ }
+ return NULL;
+}
+
+char *
+strrchr(const char *s, char c)
+{
+ const char *fnd = NULL;
+ for(; *s; s++ ){
+ if(*s == c) fnd = s;
+ }
+ return fnd;
+}
diff --git a/lib/modit/mem/mmap.c b/lib/modit/mem/mmap.c
new file mode 100644
index 0000000..83ac433
--- /dev/null
+++ b/lib/modit/mem/mmap.c
@@ -0,0 +1,11 @@
+#include "sys/mem.h"
+#include "modit/syscall.h"
+
+DEFN_SYSCALL(map, SYSCALL_MAP)
+
+void
+mmap(uintptr_t phys, uintptr_t virt)
+{
+ struct syscall_map_data data = { .phys = phys, .virt = virt, .flags = 7 };
+ _syscall_map(&data);
+}
diff --git a/root/include/ctype.h b/root/include/ctype.h
new file mode 100644
index 0000000..b7ca0a3
--- /dev/null
+++ b/root/include/ctype.h
@@ -0,0 +1,7 @@
+#ifndef CTYPE_H
+#define CTYPE_H 1
+
+int toupper(int c);
+int tolower(int c);
+
+#endif
diff --git a/root/include/stdio.h b/root/include/stdio.h
new file mode 100644
index 0000000..4cd9d6f
--- /dev/null
+++ b/root/include/stdio.h
@@ -0,0 +1,26 @@
+#ifndef STDIO_H
+#define STDIO_H 1
+
+#include <stddef.h>
+#include <stdint.h>
+#include <stdarg.h>
+
+typedef struct _file {
+ int fileno;
+ int flags;
+ intmax_t offset;
+
+ char *buff, buffof;
+} FILE;
+
+extern FILE *stdin;
+extern FILE *stdout;
+extern FILE *stderr;
+
+int printf(const char *restrict format, ...);
+int fprintf(FILE *restrict stream, const char *restrict format, ...);
+int sprintf(char *restrict str, const char *restrict format, ...);
+
+int vsnprintf(char *restrict str, size_t size, const char *restrict format, va_list ap);
+
+#endif
diff --git a/root/include/string.h b/root/include/string.h
index 75ede23..b92d69a 100644
--- a/root/include/string.h
+++ b/root/include/string.h
@@ -2,10 +2,19 @@
#define STRING_H 1
#include <stddef.h>
+#include <stdint.h>
size_t strlen(const char *s);
int strcmp(const char *a, const char *b);
char *strcpy(char *restrict dest, const char *src);
+char *strchr(const char *s, char c);
+char *strrchr(const char *s, char c);
+
+char *itoa(intmax_t value, char *str, int base);
+
+char *strlow(char *s);
+char *strupp(char *s);
+
#endif
diff --git a/root/include/sys/alloc.h b/root/include/sys/mem.h
index a674196..f4b2953 100644
--- a/root/include/sys/alloc.h
+++ b/root/include/sys/mem.h
@@ -5,5 +5,6 @@
#include <stddef.h>
void alloc(uintptr_t at, size_t size);
+void mmap(uintptr_t phys, uintptr_t virt);
#endif