Compare commits
2 Commits
dc3d80d707
...
26ff717b50
Author | SHA1 | Date | |
---|---|---|---|
26ff717b50 | |||
e6891b39cc |
1
base/scripts/init.tb
Normal file
1
base/scripts/init.tb
Normal file
@ -0,0 +1 @@
|
||||
print "this is an init script!"
|
@ -2,8 +2,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include "bitmap.h"
|
||||
|
||||
#define DIV_ROUNDUP(num, div) ((num + div - 1) / div)
|
||||
#include "util/util.h"
|
||||
|
||||
void *bitmap_toptr(BitMap *bm, size_t block) {
|
||||
uint8_t *ptr = (uint8_t *)(bm->mem_start + (block * BITMAP_BLOCK_SIZE));
|
||||
@ -17,12 +16,12 @@ size_t bitmap_toblock(BitMap *bm, void *ptr) {
|
||||
|
||||
size_t bitmap_toblock_roundup(BitMap *bm, void *ptr) {
|
||||
uint8_t *p = ptr;
|
||||
return (size_t)DIV_ROUNDUP((size_t)(p - bm->mem_start), BITMAP_BLOCK_SIZE);
|
||||
return (size_t)_DIV_ROUNDUP((size_t)(p - bm->mem_start), BITMAP_BLOCK_SIZE);
|
||||
}
|
||||
|
||||
size_t bitmap_calcsize(size_t total) {
|
||||
size_t nblocks = DIV_ROUNDUP(total, BITMAP_BLOCK_SIZE);
|
||||
size_t nbytes = DIV_ROUNDUP(nblocks, 8);
|
||||
size_t nblocks = _DIV_ROUNDUP(total, BITMAP_BLOCK_SIZE);
|
||||
size_t nbytes = _DIV_ROUNDUP(nblocks, 8);
|
||||
return nbytes;
|
||||
}
|
||||
|
||||
@ -59,7 +58,7 @@ void bitmap_markregion(BitMap *bm, void *baseptr, size_t size, bool is_used) {
|
||||
|
||||
if (is_used) {
|
||||
base = bitmap_toblock(bm, baseptr);
|
||||
size1 = DIV_ROUNDUP(size, BITMAP_BLOCK_SIZE);
|
||||
size1 = _DIV_ROUNDUP(size, BITMAP_BLOCK_SIZE);
|
||||
} else {
|
||||
base = bitmap_toblock(bm, baseptr);
|
||||
size1 = size / BITMAP_BLOCK_SIZE;
|
||||
|
@ -27,5 +27,6 @@ void hal_wait(uint32_t ms);
|
||||
#include "x86_64/cpu.h"
|
||||
#include "x86_64/intr.h"
|
||||
#include "x86_64/io.h"
|
||||
#include "x86_64/gdt.h"
|
||||
|
||||
#endif // KERNEL_HAL_HAL_H_
|
||||
|
@ -4,17 +4,12 @@
|
||||
#include "hal/hal.h"
|
||||
#include "gdt.h"
|
||||
|
||||
#define GDT_SIZE 5
|
||||
#define GDT_PRESENT 0x80
|
||||
#define GDT_TSS 0x89
|
||||
|
||||
#define KSTACK 8192
|
||||
ALIGNED(16) static uint8_t kernelstack[KSTACK];
|
||||
|
||||
#define ISTS 7
|
||||
#define ISTACK 4096
|
||||
ALIGNED(16) static uint8_t iststacks[ISTS][ISTACK];
|
||||
|
||||
typedef struct {
|
||||
uint16_t limitlow;
|
||||
uint16_t baselow;
|
||||
@ -35,19 +30,7 @@ typedef struct {
|
||||
GdtEntry tsshigh;
|
||||
} PACKED ExtendedGdt;
|
||||
|
||||
typedef struct {
|
||||
uint32_t resv0;
|
||||
uint64_t rsp0;
|
||||
uint64_t rsp1;
|
||||
uint64_t rsp2;
|
||||
uint64_t resv1;
|
||||
uint64_t ist[ISTS];
|
||||
uint64_t resv2;
|
||||
uint16_t resv3;
|
||||
uint16_t iopb_off;
|
||||
} PACKED Tss;
|
||||
|
||||
ALIGNED(16) static Tss tss = {0};
|
||||
ALIGNED(16) Tss tss = {0};
|
||||
ALIGNED(16) static ExtendedGdt gdt = {0};
|
||||
|
||||
void gdt_setentry(GdtEntry *ent, uint32_t base, uint32_t limit, uint8_t acc, uint8_t gran) {
|
||||
@ -65,18 +48,14 @@ void gdt_init(void) {
|
||||
|
||||
tss.rsp0 = (uint64_t)(kernelstack + sizeof(kernelstack));
|
||||
|
||||
for (size_t i = 0; i < ISTS; i++) {
|
||||
tss.ist[i] = (uint64_t)(iststacks[i] + sizeof(iststacks[i]));
|
||||
}
|
||||
|
||||
uint64_t tss_base = (uint64_t)&tss;
|
||||
uint32_t tss_limit = sizeof(tss) - 1;
|
||||
|
||||
gdt_setentry(&gdt.old[0], 0, 0, 0, 0);
|
||||
gdt_setentry(&gdt.old[1], 0, 0xFFFFF, 0x9a, 0xA0);
|
||||
gdt_setentry(&gdt.old[2], 0, 0xFFFFF, 0x92, 0xA0);
|
||||
gdt_setentry(&gdt.old[2], 0, 0xFFFFF, 0x92, 0xC0);
|
||||
gdt_setentry(&gdt.old[3], 0, 0xFFFFF, 0xfa, 0xA0);
|
||||
gdt_setentry(&gdt.old[4], 0, 0xFFFFF, 0xf2, 0xA0);
|
||||
gdt_setentry(&gdt.old[4], 0, 0xFFFFF, 0xf2, 0xC0);
|
||||
|
||||
gdt_setentry(&gdt.tsslow, tss_base & 0xFFFFFFFF, tss_limit, GDT_PRESENT | GDT_TSS, 0x0);
|
||||
|
||||
|
@ -7,6 +7,20 @@
|
||||
#define UDATA 0x20
|
||||
#define TSS 0x28
|
||||
|
||||
typedef struct {
|
||||
uint32_t resv0;
|
||||
uint64_t rsp0;
|
||||
uint64_t rsp1;
|
||||
uint64_t rsp2;
|
||||
uint64_t resv1;
|
||||
uint64_t ist[7];
|
||||
uint64_t resv2;
|
||||
uint16_t resv3;
|
||||
uint16_t iopb_off;
|
||||
} PACKED Tss;
|
||||
|
||||
ALIGNED(16) extern Tss tss;
|
||||
|
||||
void gdt_init(void);
|
||||
|
||||
#endif // HAL_GDT_H_
|
||||
|
@ -44,10 +44,10 @@ typedef struct {
|
||||
ALIGNED(0x10) static IdtGate idtgates[ENTRIES] = {0};
|
||||
static Idt idt = {0};
|
||||
|
||||
void idt_setentry(int i, uint64_t handler, uint8_t ist, uint8_t flags) {
|
||||
void idt_setentry(int i, uint64_t handler, uint8_t flags) {
|
||||
idtgates[i].intrlow = handler & 0xffff;
|
||||
idtgates[i].kernelcs = KCODE;
|
||||
idtgates[i].ist = ist;
|
||||
idtgates[i].ist = 0;
|
||||
idtgates[i].attrs = flags;
|
||||
idtgates[i].intrmid = (handler >> 16) & 0xFFFF;
|
||||
idtgates[i].intrhigh = (handler >> 32) & 0xFFFFFFFF;
|
||||
@ -76,60 +76,60 @@ static const char *exceptions[] = {
|
||||
};
|
||||
|
||||
void intr_init(void) {
|
||||
#define MKINTR(N, IST) \
|
||||
#define MKINTR(N) \
|
||||
extern void intr_vec##N(void); \
|
||||
idt_setentry(N, (uint64_t)&intr_vec##N, IST, 0x8E);
|
||||
idt_setentry(N, (uint64_t)&intr_vec##N, 0x8E);
|
||||
|
||||
MKINTR(0, 0);
|
||||
MKINTR(1, 0);
|
||||
MKINTR(2, 2);
|
||||
MKINTR(4, 0);
|
||||
MKINTR(5, 0);
|
||||
MKINTR(6, 0);
|
||||
MKINTR(7, 0);
|
||||
MKINTR(8, 1);
|
||||
MKINTR(9, 0);
|
||||
MKINTR(10, 0);
|
||||
MKINTR(11, 0);
|
||||
MKINTR(12, 0);
|
||||
MKINTR(13, 0);
|
||||
MKINTR(14, 0);
|
||||
MKINTR(15, 0);
|
||||
MKINTR(16, 0);
|
||||
MKINTR(17, 0);
|
||||
MKINTR(18, 0);
|
||||
MKINTR(19, 0);
|
||||
MKINTR(20, 0);
|
||||
MKINTR(21, 0);
|
||||
MKINTR(22, 0);
|
||||
MKINTR(23, 0);
|
||||
MKINTR(24, 0);
|
||||
MKINTR(25, 0);
|
||||
MKINTR(26, 0);
|
||||
MKINTR(27, 0);
|
||||
MKINTR(28, 0);
|
||||
MKINTR(29, 0);
|
||||
MKINTR(30, 0);
|
||||
MKINTR(31, 0);
|
||||
MKINTR(32, 0);
|
||||
MKINTR(33, 0);
|
||||
MKINTR(34, 0);
|
||||
MKINTR(35, 0);
|
||||
MKINTR(36, 0);
|
||||
MKINTR(37, 0);
|
||||
MKINTR(38, 0);
|
||||
MKINTR(39, 0);
|
||||
MKINTR(40, 3);
|
||||
MKINTR(41, 0);
|
||||
MKINTR(42, 0);
|
||||
MKINTR(43, 0);
|
||||
MKINTR(44, 0);
|
||||
MKINTR(45, 0);
|
||||
MKINTR(46, 0);
|
||||
MKINTR(47, 0);
|
||||
MKINTR(0);
|
||||
MKINTR(1);
|
||||
MKINTR(2);
|
||||
MKINTR(4);
|
||||
MKINTR(5);
|
||||
MKINTR(6);
|
||||
MKINTR(7);
|
||||
MKINTR(8);
|
||||
MKINTR(9);
|
||||
MKINTR(10);
|
||||
MKINTR(11);
|
||||
MKINTR(12);
|
||||
MKINTR(13);
|
||||
MKINTR(14);
|
||||
MKINTR(15);
|
||||
MKINTR(16);
|
||||
MKINTR(17);
|
||||
MKINTR(18);
|
||||
MKINTR(19);
|
||||
MKINTR(20);
|
||||
MKINTR(21);
|
||||
MKINTR(22);
|
||||
MKINTR(23);
|
||||
MKINTR(24);
|
||||
MKINTR(25);
|
||||
MKINTR(26);
|
||||
MKINTR(27);
|
||||
MKINTR(28);
|
||||
MKINTR(29);
|
||||
MKINTR(30);
|
||||
MKINTR(31);
|
||||
MKINTR(32);
|
||||
MKINTR(33);
|
||||
MKINTR(34);
|
||||
MKINTR(35);
|
||||
MKINTR(36);
|
||||
MKINTR(37);
|
||||
MKINTR(38);
|
||||
MKINTR(39);
|
||||
MKINTR(40);
|
||||
MKINTR(41);
|
||||
MKINTR(42);
|
||||
MKINTR(43);
|
||||
MKINTR(44);
|
||||
MKINTR(45);
|
||||
MKINTR(46);
|
||||
MKINTR(47);
|
||||
|
||||
extern void intr_vec128(void);
|
||||
idt_setentry(0x80, (uint64_t)&intr_vec128, 0, 0xEE);
|
||||
idt_setentry(0x80, (uint64_t)&intr_vec128, 0xEE);
|
||||
|
||||
idt_init();
|
||||
}
|
||||
@ -165,6 +165,8 @@ void hal_syscalldispatch(IntrStackFrame *frame) {
|
||||
frame->regs.rax = E_BADSYSCALL;
|
||||
return;
|
||||
}
|
||||
uint64_t cr3;
|
||||
asm volatile("mov %%cr3, %0" : "=r"(cr3));
|
||||
int32_t ret = fn(frame->regs.rdi, frame->regs.rsi, frame->regs.rdx,
|
||||
frame->regs.r10, frame->regs.r8, frame->regs.r9);
|
||||
|
||||
|
@ -90,8 +90,10 @@ Proc *proc_spawnkern(void (*ent)(void), char *name) {
|
||||
proc->kern = true;
|
||||
|
||||
uint8_t *sp = (uint8_t *)pmm_alloc(PROC_STACKBLOCKS) + PROC_STACKSIZE;
|
||||
uint8_t *kstackp = (uint8_t *)pmm_alloc(PROC_STACKBLOCKS) + PROC_STACKSIZE;
|
||||
proc->platformdata.kstack = kstackp;
|
||||
proc->platformdata.pstack = sp;
|
||||
|
||||
proc->platformdata.kstack = sp;
|
||||
hal_memset(&proc->platformdata.trapframe, 0, sizeof(proc->platformdata.trapframe));
|
||||
|
||||
proc->platformdata.trapframe.ss = 0x10;
|
||||
@ -99,7 +101,7 @@ Proc *proc_spawnkern(void (*ent)(void), char *name) {
|
||||
proc->platformdata.trapframe.rflags = 0x202;
|
||||
proc->platformdata.trapframe.cs = 0x08;
|
||||
proc->platformdata.trapframe.rip = (uint64_t)ent;
|
||||
proc->platformdata.cr3 = hal_vmm_current_cr3();
|
||||
proc->platformdata.cr3 = KERNEL_CR3;
|
||||
proc->state = PROC_EMBRYO;
|
||||
proc->pid = pids++;
|
||||
spinlock_init(&proc->bcast_pipes.spinlock);
|
||||
@ -145,19 +147,22 @@ Proc *proc_spawnuser(char *mountpoint, char *path) {
|
||||
|
||||
uint8_t *sp = (uint8_t *)pmm_alloc(PROC_STACKBLOCKS) + PROC_STACKSIZE;
|
||||
uint8_t *spbase = sp - PROC_STACKSIZE;
|
||||
uint8_t *kstackp = (uint8_t *)pmm_alloc(PROC_STACKBLOCKS) + PROC_STACKSIZE;
|
||||
proc->platformdata.kstack = kstackp;
|
||||
proc->platformdata.pstack = sp;
|
||||
|
||||
proc->platformdata.kstack = sp;
|
||||
hal_memset(&proc->platformdata.trapframe, 0, sizeof(proc->platformdata.trapframe));
|
||||
|
||||
proc->platformdata.cr3 = hal_vmm_userproc_pml4(proc);
|
||||
|
||||
uint32_t flags = HAL_PG_RW | HAL_PG_USER | HAL_PG_PRESENT;
|
||||
|
||||
hal_vmm_map_range(VIRT(proc->platformdata.cr3), spbase, spbase, PROC_STACKSIZE, flags);
|
||||
VasRange *range = dlmalloc(sizeof(*range));
|
||||
range->virtstart = spbase;
|
||||
range->physstart = spbase;
|
||||
range->size = PROC_STACKSIZE;
|
||||
range->pgflags = flags;
|
||||
|
||||
LL_APPEND(proc->vas, range);
|
||||
|
||||
ElfAuxval aux = proc_load_elf_segs(proc, data);
|
||||
@ -216,15 +221,16 @@ void proc_reaper(void) {
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < PROC_PIPEHANDLES_MAX; i++) {
|
||||
if (zombie->pipes[i] != NULL && zombie->pipes[i]->ownerpid == zombie->pid) {
|
||||
dlfree(zombie->pipes[i]);
|
||||
ipc_pipefree(zombie->pipes[i]);
|
||||
zombie->pipes[i] = NULL;
|
||||
}
|
||||
}
|
||||
/* for (size_t i = 0; i < PROC_PIPEHANDLES_MAX; i++) { */
|
||||
/* if (zombie->pipes[i] != NULL && zombie->pipes[i]->ownerpid == zombie->pid) { */
|
||||
/* dlfree(zombie->pipes[i]); */
|
||||
/* ipc_pipefree(zombie->pipes[i]); */
|
||||
/* zombie->pipes[i] = NULL; */
|
||||
/* } */
|
||||
/* } */
|
||||
|
||||
pmm_free((uintptr_t)(zombie->platformdata.kstack - PROC_STACKSIZE), PROC_STACKBLOCKS);
|
||||
pmm_free((uintptr_t)(zombie->platformdata.pstack - PROC_STACKSIZE), PROC_STACKBLOCKS);
|
||||
|
||||
if (!zombie->kern) {
|
||||
VasRange *vashead = zombie->vas;
|
||||
@ -258,8 +264,6 @@ void proc_reaper(void) {
|
||||
}
|
||||
}
|
||||
|
||||
extern void hal_zombiespin(void);
|
||||
|
||||
void proc_sched(void *cpustate) {
|
||||
hal_intr_disable();
|
||||
sched_ticks++;
|
||||
@ -274,6 +278,7 @@ void proc_sched(void *cpustate) {
|
||||
proc_reaper();
|
||||
}
|
||||
|
||||
tss.rsp0 = (uint64_t)VIRT(PROCS.current->platformdata.kstack);
|
||||
hal_switchproc(&PROCS.current->platformdata.trapframe, (void *)PROCS.current->platformdata.cr3);
|
||||
}
|
||||
|
||||
@ -331,5 +336,6 @@ void proc_init(void) {
|
||||
proc_register(init);
|
||||
init->state = PROC_READY;
|
||||
|
||||
tss.rsp0 = (uint64_t)VIRT(PROCS.current->platformdata.kstack);
|
||||
hal_switchproc(&PROCS.current->platformdata.trapframe, (void *)PROCS.current->platformdata.cr3);
|
||||
}
|
||||
|
@ -24,6 +24,7 @@
|
||||
typedef struct {
|
||||
IntrStackFrame trapframe;
|
||||
uint8_t *kstack;
|
||||
uint8_t *pstack;
|
||||
PgTable *cr3;
|
||||
} ProcPlatformData;
|
||||
|
||||
|
@ -61,9 +61,9 @@ int32_t SYSCALL5(sys_processctl, pid1, cmd1, arg1, arg2, arg3) {
|
||||
}
|
||||
|
||||
size_t argslen = arg3;
|
||||
char **args = (char **)arg2;
|
||||
char *(*args)[] = (char *(*)[])arg2;
|
||||
for (size_t i = 0; i < argslen; i++) {
|
||||
PROC_ARG(newproc, args[i]);
|
||||
PROC_ARG(newproc, (*args)[i]);
|
||||
}
|
||||
|
||||
proc_register(newproc);
|
||||
|
@ -16,4 +16,22 @@ enum {
|
||||
E_INVALIDOPER = -11,
|
||||
};
|
||||
|
||||
static const char *_ERROR_STRINGS[] = {
|
||||
"OK",
|
||||
"Out of memory",
|
||||
"Unknown filesystem type",
|
||||
"No entry",
|
||||
"Out of bounds access",
|
||||
"Unknown storage device type",
|
||||
"TODO",
|
||||
"I/O error",
|
||||
"System call error",
|
||||
"Perform scheduling (internal flag)",
|
||||
"Invalid argument",
|
||||
"Invalid operation",
|
||||
};
|
||||
|
||||
#define ERRSTRING_INDEX(ioh) ((size_t)((ioh) < 0 ? (ioh) * (-1) : (ioh)))
|
||||
#define ERRSTRING(ioh) (_ERROR_STRINGS[ERRSTRING_INDEX(ioh)])
|
||||
|
||||
#endif // ERRORS_H_
|
||||
|
@ -12,10 +12,12 @@ SRCFILES := $(call GRABSRC, \
|
||||
printf \
|
||||
dlmalloc \
|
||||
sync \
|
||||
args \
|
||||
)
|
||||
|
||||
CFLAGS += -isystem $(ROOT)/share -isystem $(ROOT)/ulib -isystem $(ROOT)/std/include \
|
||||
-DPRINTF_INCLUDE_CONFIG_H=1 -DLACKS_STRING_H=1 -include $(ROOT)/ulib/string/string.h
|
||||
-isystem $(ROOT)/ulib/std -DPRINTF_INCLUDE_CONFIG_H=1 \
|
||||
-DULIB_FLOAT_SUPPORT=0
|
||||
|
||||
ASFILES := $(call GET_ASFILES, $(SRCFILES))
|
||||
CFILES := $(call GET_CFILES, $(SRCFILES))
|
||||
|
@ -5,25 +5,27 @@
|
||||
#include <errors.h>
|
||||
#include <dlmalloc/malloc.h>
|
||||
#include <uprintf.h>
|
||||
#include <log.h>
|
||||
|
||||
extern void main(void);
|
||||
|
||||
extern uint8_t _bss_start[];
|
||||
extern uint8_t _bss_end[];
|
||||
void clearbss(void) {
|
||||
extern uint8_t _bss_start;
|
||||
extern uint8_t _bss_end;
|
||||
|
||||
void bss_clear(void) {
|
||||
uint8_t *p = _bss_start;
|
||||
while (p != _bss_end) {
|
||||
*p = 0;
|
||||
p++;
|
||||
uint8_t *ps = &_bss_start;
|
||||
uint8_t *pe = &_bss_end;
|
||||
size_t sz = pe - ps;
|
||||
for (size_t i = 0; i < sz; i++) {
|
||||
ps[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static char **_args;
|
||||
static size_t _argslen;
|
||||
char **_args;
|
||||
size_t _argslen;
|
||||
|
||||
char **args(void) {
|
||||
return _args;
|
||||
return (char **)_args;
|
||||
}
|
||||
|
||||
size_t argslen(void) {
|
||||
@ -32,7 +34,7 @@ size_t argslen(void) {
|
||||
|
||||
// ulib initialization goes here
|
||||
void _premain(void) {
|
||||
bss_clear();
|
||||
clearbss();
|
||||
|
||||
_argslen = processctl(-1, PCTL_ARGLEN, 0, 0, 0);
|
||||
_args = dlmalloc(_argslen * sizeof(*_args));
|
||||
@ -45,9 +47,8 @@ void _premain(void) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (processctl(-1, PCTL_ARGV, (uint64_t)_args, _argslen, 0) != E_OK) {
|
||||
return;
|
||||
}
|
||||
|
||||
processctl(-1, PCTL_ARGV, (uint64_t)_args, _argslen, 0);
|
||||
|
||||
main();
|
||||
}
|
||||
|
@ -9,4 +9,5 @@ _start:
|
||||
movq $2, %rax // sys processctl
|
||||
movq $-1, %rdi // self magic num
|
||||
movq $0, %rsi // kill cmd
|
||||
|
||||
int $0x80
|
||||
|
@ -1,7 +0,0 @@
|
||||
#ifndef ULIB_ARGS_H_
|
||||
#define ULIB_ARGS_H_
|
||||
|
||||
char **args(void);
|
||||
size_t argslen(void);
|
||||
|
||||
#endif // ULIB_ARGS_H_
|
32
ulib/args/args.c
Normal file
32
ulib/args/args.c
Normal file
@ -0,0 +1,32 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <args/args.h>
|
||||
#include <string/conv.h>
|
||||
#include <string/string.h>
|
||||
#include <uprintf.h>
|
||||
|
||||
int32_t parse_args(char **argv, size_t argc, Arg *defs) {
|
||||
for (size_t i = 0; i < argc; i++) {
|
||||
if (argv[i][0] == '-' && argv[i][1] != '\0') {
|
||||
char *optname = argv[i];
|
||||
size_t j = 0;
|
||||
while (!defs[j].end) {
|
||||
Arg *def = &defs[j];
|
||||
if (string_strcmp(def->shortname, optname) == 0) {
|
||||
if (i < argc - 1) {
|
||||
switch (def->expected_value) {
|
||||
case ARG_STRING:
|
||||
*((char **)def->ptr) = argv[i+1];
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
j++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ARGP_OK;
|
||||
}
|
33
ulib/args/args.h
Normal file
33
ulib/args/args.h
Normal file
@ -0,0 +1,33 @@
|
||||
#ifndef ULIB_ARGS_H_
|
||||
#define ULIB_ARGS_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
char **args(void);
|
||||
size_t argslen(void);
|
||||
|
||||
typedef struct {
|
||||
char *shortname;
|
||||
enum { ARG_STRING } expected_value;
|
||||
void *ptr;
|
||||
bool end;
|
||||
} Arg;
|
||||
|
||||
#define ARG(sn, ev, p) ((Arg){ \
|
||||
.shortname = (sn), \
|
||||
.expected_value = (ev), \
|
||||
.ptr = (void *)(p), \
|
||||
.end = false, \
|
||||
})
|
||||
|
||||
#define ARG_END() ((Arg){ .end = true, })
|
||||
|
||||
enum {
|
||||
ARGP_OK = 0,
|
||||
};
|
||||
|
||||
int32_t parse_args(char **argv, size_t argc, Arg *defs);
|
||||
|
||||
#endif // ULIB_ARGS_H_
|
@ -53,24 +53,40 @@ int INITIAL_LOCK(SpinLock *sl) {
|
||||
static MLOCK_T malloc_global_mutex = { 0 };
|
||||
|
||||
#define PAGE_SIZE 0x1000
|
||||
|
||||
static uint8_t *heap_start = (uint8_t *)NULL;
|
||||
static uint8_t *heap_end = NULL;
|
||||
|
||||
void *sbrk(long inc) {
|
||||
static size_t _roundpage(size_t sz) {
|
||||
return (sz + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
|
||||
}
|
||||
|
||||
void *sbrk(ptrdiff_t inc) {
|
||||
if (!heap_end) {
|
||||
heap_end = heap_start;
|
||||
}
|
||||
|
||||
if (inc == 0) {
|
||||
return heap_end;
|
||||
}
|
||||
|
||||
uint8_t *new_heap_end = heap_end + inc;
|
||||
if (new_heap_end > heap_end) {
|
||||
size_t size = new_heap_end - heap_end;
|
||||
uint8_t *oldh = heap_end;
|
||||
uint8_t *newh = heap_end + inc;
|
||||
|
||||
uint8_t *out = NULL;
|
||||
int32_t ret = mman_map(heap_end, size, MMAN_MAP_PF_RW, 0, &out);
|
||||
if (ret != E_OK) {
|
||||
if (inc > 0) {
|
||||
size_t allocsz = _roundpage((size_t)(newh - oldh));
|
||||
uint8_t *maddr = NULL;
|
||||
int32_t ret = mman_map(NULL, allocsz, MMAN_MAP_PF_RW, 0, &maddr);
|
||||
if (ret != E_OK || maddr == NULL) {
|
||||
return (void *)-1;
|
||||
}
|
||||
string_memset(out, 0, size);
|
||||
if (!heap_start) {
|
||||
heap_start = maddr;
|
||||
}
|
||||
oldh = heap_end ? heap_end : maddr;
|
||||
heap_end = oldh + allocsz;
|
||||
} else {
|
||||
heap_end = newh;
|
||||
}
|
||||
heap_end = new_heap_end;
|
||||
return (void *)heap_end;
|
||||
return oldh;
|
||||
}
|
||||
|
17
ulib/log.h
Normal file
17
ulib/log.h
Normal file
@ -0,0 +1,17 @@
|
||||
#ifndef ULIB_LOG_H_
|
||||
#define ULIB_LOG_H_
|
||||
|
||||
#include <uprintf.h>
|
||||
|
||||
enum {
|
||||
LOG_ERR,
|
||||
LOG_DBG,
|
||||
LOG_INF,
|
||||
LOG_WRN,
|
||||
};
|
||||
|
||||
static const char *_LOG_STR[] = { "ERROR", "DEBUG", "INFO", "WARNING" };
|
||||
|
||||
#define LOG(mode, fmt, ...) uprintf("%s: "fmt, _LOG_STR[(mode)], ##__VA_ARGS__);
|
||||
|
||||
#endif // ULIB_LOG_H_
|
25
ulib/machine/limits.h
Normal file
25
ulib/machine/limits.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef ULIB_MACHINE_LIMITS_H_
|
||||
#define ULIB_MACHINE_LIMITS_H_
|
||||
|
||||
#define CHAR_BIT (8)
|
||||
#define SHORT_BIT (16)
|
||||
#define WCHAR_BIT (16)
|
||||
#define INT_BIT (32)
|
||||
#define LONG_LONG_BIT (64)
|
||||
#define SCHAR_MIN (-128)
|
||||
#define SCHAR_MAX (127)
|
||||
#define UCHAR_MAX (255)
|
||||
#define SHRT_MIN (-32768)
|
||||
#define SHRT_MAX (32767)
|
||||
#define USHRT_MAX (65535)
|
||||
#define INT_MIN (-2147483648)
|
||||
#define INT_MAX (2147483647)
|
||||
#define UINT_MAX (4294967295U)
|
||||
#define LONG_MAX (2147483647L)
|
||||
#define LONG_MIN (-2147483648L)
|
||||
#define ULONG_MAX (18446744073709551615UL)
|
||||
#define LLONG_MAX (9223372036854775807LL)
|
||||
#define LLONG_MIN (-9223372036854775808LL)
|
||||
#define ULLONG_MAX (18446744073709551615ULL)
|
||||
|
||||
#endif // ULIB_MACHINE_LIMITS_H_
|
6
ulib/std/limits.h
Normal file
6
ulib/std/limits.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef ULIB_STD_LIMITS_H_
|
||||
#define ULIB_STD_LIMITS_H_
|
||||
|
||||
#include <machine/limits.h>
|
||||
|
||||
#endif // ULIB_STD_LIMITS_H_
|
17
ulib/std/stdlib.h
Normal file
17
ulib/std/stdlib.h
Normal file
@ -0,0 +1,17 @@
|
||||
#ifndef ULIB_STD_STDLIB_H_
|
||||
#define ULIB_STD_STDLIB_H_
|
||||
|
||||
#include <uprintf.h>
|
||||
#include <string/conv.h>
|
||||
|
||||
#define printf uprintf
|
||||
#define sprintf usprintf
|
||||
#define vsprintf uvsprintf
|
||||
#define snprintf usnprintf
|
||||
#define vsnprintf uvsnprintf
|
||||
#define vprintf uvprintf
|
||||
|
||||
#define strtoul string_conv_strtoul
|
||||
#define strtol string_conv_strtol
|
||||
|
||||
#endif // ULIB_STD_STDLIB_H_
|
17
ulib/std/string.h
Normal file
17
ulib/std/string.h
Normal file
@ -0,0 +1,17 @@
|
||||
#ifndef ULIB_STD_STRING_H_
|
||||
#define ULIB_STD_STRING_H_
|
||||
|
||||
#include <string/string.h>
|
||||
|
||||
#define memset string_memset
|
||||
#define memcpy string_memcpy
|
||||
#define strlen string_len
|
||||
#define memcmp string_memcmp
|
||||
#define strcmp string_strcmp
|
||||
#define strcspn string_strcspn
|
||||
#define strspn string_strspn
|
||||
#define strcpy string_strcpy
|
||||
#define strchr string_strchr
|
||||
#define strncmp string_strncmp
|
||||
|
||||
#endif // ULIB_STD_STRING_H_
|
17
ulib/string/char.h
Normal file
17
ulib/string/char.h
Normal file
@ -0,0 +1,17 @@
|
||||
#ifndef ULIB_STRING_CHAR_H_
|
||||
#define ULIB_STRING_CHAR_H_
|
||||
|
||||
#define string_chr_islower(c) ((c) >= 'a' && (c) <= 'z')
|
||||
#define string_chr_isupper(c) ((c) >= 'A' && (c) <= 'Z')
|
||||
#define string_chr_isalpha(c) (string_chr_islower((c)) || string_chr_isupper((c)))
|
||||
#define string_chr_isctl(c) (((c) >= 0 && (c) <= 0x1f) || (c) == 0x7f)
|
||||
#define string_chr_isdigit(c) ((c) >= '0' && (c) <= '9')
|
||||
#define string_chr_isalnum(c) (string_chr_isalpha((c)) || string_chr_isdigit((c)))
|
||||
#define string_chr_isprint(c) ((c) > 0x1f && (c) < 0x7f)
|
||||
#define string_chr_isspace(c) ((c) == ' ' || (c) == '\f' || (c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == '\v')
|
||||
#define string_chr_tolower(c) (string_chr_isupper((c)) ? ((c) - 'A' + 'a') : (c))
|
||||
#define string_chr_toupper(c) (string_chr_islower((c)) ? ((c) - 'a' + 'A') : (c))
|
||||
#define string_chr_isascii(c) (!((c) < 0 || (c) > 0x7f))
|
||||
#define string_chr_isxdigit(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
|
||||
|
||||
#endif // ULIB_STRING_CHAR_H_
|
159
ulib/string/conv.c
Normal file
159
ulib/string/conv.c
Normal file
@ -0,0 +1,159 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <machine/limits.h>
|
||||
#include <string/char.h>
|
||||
#include <uprintf.h>
|
||||
|
||||
// https://android.googlesource.com/platform/bionic/+/ics-mr0/libc/stdlib/strtoul.c
|
||||
unsigned long string_conv_strtoul(const char *nptr, char **endptr, int base)
|
||||
{
|
||||
const char *s;
|
||||
unsigned long acc, cutoff;
|
||||
int c;
|
||||
int neg, any, cutlim;
|
||||
/*
|
||||
* See strtol for comments as to the logic used.
|
||||
*/
|
||||
s = nptr;
|
||||
do {
|
||||
c = (unsigned char) *s++;
|
||||
} while (string_chr_isspace(c));
|
||||
if (c == '-') {
|
||||
neg = 1;
|
||||
c = *s++;
|
||||
} else {
|
||||
neg = 0;
|
||||
if (c == '+')
|
||||
c = *s++;
|
||||
}
|
||||
if ((base == 0 || base == 16) &&
|
||||
c == '0' && (*s == 'x' || *s == 'X')) {
|
||||
c = s[1];
|
||||
s += 2;
|
||||
base = 16;
|
||||
}
|
||||
if (base == 0)
|
||||
base = c == '0' ? 8 : 10;
|
||||
cutoff = ULONG_MAX / (unsigned long)base;
|
||||
cutlim = ULONG_MAX % (unsigned long)base;
|
||||
for (acc = 0, any = 0;; c = (unsigned char) *s++) {
|
||||
if (string_chr_isdigit(c))
|
||||
c -= '0';
|
||||
else if (string_chr_isalpha(c))
|
||||
c -= string_chr_isupper(c) ? 'A' - 10 : 'a' - 10;
|
||||
else
|
||||
break;
|
||||
if (c >= base)
|
||||
break;
|
||||
if (any < 0)
|
||||
continue;
|
||||
if (acc > cutoff || (acc == cutoff && c > cutlim)) {
|
||||
any = -1;
|
||||
acc = ULONG_MAX;
|
||||
} else {
|
||||
any = 1;
|
||||
acc *= (unsigned long)base;
|
||||
acc += c;
|
||||
}
|
||||
}
|
||||
if (neg && any > 0)
|
||||
acc = -acc;
|
||||
if (endptr != 0)
|
||||
*endptr = (char *) (any ? s - 1 : nptr);
|
||||
return (acc);
|
||||
}
|
||||
|
||||
// https://android.googlesource.com/platform/bionic/+/ics-mr0/libc/stdlib/strtol.c
|
||||
long string_conv_strtol(const char *nptr, char **endptr, int base)
|
||||
{
|
||||
const char *s;
|
||||
long acc, cutoff;
|
||||
int c;
|
||||
int neg, any, cutlim;
|
||||
/*
|
||||
* Skip white space and pick up leading +/- sign if any.
|
||||
* If base is 0, allow 0x for hex and 0 for octal, else
|
||||
* assume decimal; if base is already 16, allow 0x.
|
||||
*/
|
||||
s = nptr;
|
||||
do {
|
||||
c = (unsigned char) *s++;
|
||||
} while (string_chr_isspace(c));
|
||||
if (c == '-') {
|
||||
neg = 1;
|
||||
c = *s++;
|
||||
} else {
|
||||
neg = 0;
|
||||
if (c == '+')
|
||||
c = *s++;
|
||||
}
|
||||
if ((base == 0 || base == 16) &&
|
||||
c == '0' && (*s == 'x' || *s == 'X')) {
|
||||
c = s[1];
|
||||
s += 2;
|
||||
base = 16;
|
||||
}
|
||||
if (base == 0)
|
||||
base = c == '0' ? 8 : 10;
|
||||
/*
|
||||
* Compute the cutoff value between legal numbers and illegal
|
||||
* numbers. That is the largest legal value, divided by the
|
||||
* base. An input number that is greater than this value, if
|
||||
* followed by a legal input character, is too big. One that
|
||||
* is equal to this value may be valid or not; the limit
|
||||
* between valid and invalid numbers is then based on the last
|
||||
* digit. For instance, if the range for longs is
|
||||
* [-2147483648..2147483647] and the input base is 10,
|
||||
* cutoff will be set to 214748364 and cutlim to either
|
||||
* 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
|
||||
* a value > 214748364, or equal but the next digit is > 7 (or 8),
|
||||
* the number is too big, and we will return a range error.
|
||||
*
|
||||
* Set any if any `digits' consumed; make it negative to indicate
|
||||
* overflow.
|
||||
*/
|
||||
cutoff = neg ? LONG_MIN : LONG_MAX;
|
||||
cutlim = cutoff % base;
|
||||
cutoff /= base;
|
||||
if (neg) {
|
||||
if (cutlim > 0) {
|
||||
cutlim -= base;
|
||||
cutoff += 1;
|
||||
}
|
||||
cutlim = -cutlim;
|
||||
}
|
||||
for (acc = 0, any = 0;; c = (unsigned char) *s++) {
|
||||
if (string_chr_isdigit(c))
|
||||
c -= '0';
|
||||
else if (string_chr_isalpha(c))
|
||||
c -= string_chr_isupper(c) ? 'A' - 10 : 'a' - 10;
|
||||
else
|
||||
break;
|
||||
if (c >= base)
|
||||
break;
|
||||
if (any < 0)
|
||||
continue;
|
||||
if (neg) {
|
||||
if (acc < cutoff || (acc == cutoff && c > cutlim)) {
|
||||
any = -1;
|
||||
acc = LONG_MIN;
|
||||
} else {
|
||||
any = 1;
|
||||
acc *= base;
|
||||
acc -= c;
|
||||
}
|
||||
} else {
|
||||
if (acc > cutoff || (acc == cutoff && c > cutlim)) {
|
||||
any = -1;
|
||||
acc = LONG_MAX;
|
||||
} else {
|
||||
any = 1;
|
||||
acc *= base;
|
||||
acc += c;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (endptr != 0)
|
||||
*endptr = (char *) (any ? s - 1 : nptr);
|
||||
return (acc);
|
||||
}
|
7
ulib/string/conv.h
Normal file
7
ulib/string/conv.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef ULIB_STRING_CONV_H_
|
||||
#define ULIB_STRING_CONV_H_
|
||||
|
||||
unsigned long string_conv_strtoul(const char *nptr, char **endptr, int base);
|
||||
long string_conv_strtol(const char *nptr, char **endptr, int base);
|
||||
|
||||
#endif // ULIB_STRING_CONV_H_
|
@ -111,3 +111,22 @@ char *string_strchr(const char *s, int c) {
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/questions/32560167/strncmp-implementation
|
||||
int string_strncmp( const char * s1, const char * s2, size_t n )
|
||||
{
|
||||
while ( n && *s1 && ( *s1 == *s2 ) )
|
||||
{
|
||||
++s1;
|
||||
++s2;
|
||||
--n;
|
||||
}
|
||||
if ( n == 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return ( *(unsigned char *)s1 - *(unsigned char *)s2 );
|
||||
}
|
||||
}
|
||||
|
@ -6,9 +6,6 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define memset string_memset
|
||||
#define memcpy string_memcpy
|
||||
|
||||
size_t string_len(const char *s);
|
||||
void *string_memset(void *p, int c, size_t n);
|
||||
void *string_memcpy(void *dst, const void *src, size_t n);
|
||||
@ -18,6 +15,7 @@ size_t string_strcspn(const char *s, const char *reject);
|
||||
size_t string_strspn(const char *s, const char *accept);
|
||||
char *string_strcpy(char *dest, const char *src);
|
||||
char *string_strchr(const char *s, int c);
|
||||
int string_strncmp(const char * s1, const char * s2, size_t n);
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -21,7 +21,7 @@ uint64_t syscall(uint64_t num, uint64_t arg1, uint64_t arg2,
|
||||
[ARG4]"r"(arg4),
|
||||
[ARG5]"r"(arg5),
|
||||
[ARG6]"r"(arg6)
|
||||
: "%rax", "%rdi", "%rsi", "%rdx", "%r10", "%r8", "%r9", "memory"
|
||||
: "rax", "rdi", "rsi", "rdx", "r10", "r8", "r9", "memory"
|
||||
);
|
||||
return ret;
|
||||
}
|
||||
|
@ -2,6 +2,10 @@
|
||||
#include <system/system.h>
|
||||
#include <syscall/syscall.h>
|
||||
#include <sysdefs/syscall.h>
|
||||
#include <sysdefs/ioctl.h>
|
||||
#include <sysdefs/processctl.h>
|
||||
#include <sysdefs/ipcpipe.h>
|
||||
#include <uprintf.h>
|
||||
|
||||
void debugprint(const char *string) {
|
||||
syscall(SYS_DEBUGPRINT, (uint64_t)string, 0, 0, 0, 0, 0);
|
||||
|
6
ulib/util/util.h
Normal file
6
ulib/util/util.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef ULIB_UTIL_UTIL_H_
|
||||
#define ULIB_UTIL_UTIL_H_
|
||||
|
||||
#define ARRLEN(X) (sizeof((X))/sizeof((X)[0]))
|
||||
|
||||
#endif // ULIB_UTIL_UTIL_H_
|
@ -1,5 +1,6 @@
|
||||
CFLAGS := -ffreestanding -Wall -Wextra -g -fcommon -nostdinc \
|
||||
-isystem $(ROOT)/std/include -isystem $(ROOT)/ulib -isystem $(ROOT)/share
|
||||
-isystem $(ROOT)/std/include -isystem $(ROOT)/ulib -isystem $(ROOT)/share \
|
||||
-isystem $(ROOT)/ulib/std
|
||||
|
||||
CURRENT_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
|
||||
|
||||
|
@ -5,23 +5,23 @@ SECTIONS {
|
||||
|
||||
.text ALIGN(4K):
|
||||
{
|
||||
*(.text .text.*)
|
||||
*(.text .text*)
|
||||
}
|
||||
|
||||
.rodata (READONLY): ALIGN(4K)
|
||||
{
|
||||
*(.rodata .rodata.*)
|
||||
*(.rodata .rodata*)
|
||||
}
|
||||
|
||||
.data ALIGN(4K):
|
||||
{
|
||||
*(.data .data.*)
|
||||
*(.data .data*)
|
||||
}
|
||||
|
||||
.bss ALIGN(4K):
|
||||
{
|
||||
_bss_start = .;
|
||||
*(.bss .bss.*)
|
||||
*(.bss .bss*)
|
||||
_bss_end = .;
|
||||
}
|
||||
}
|
||||
|
@ -1,60 +1,37 @@
|
||||
#include <stdint.h>
|
||||
#include <string/string.h>
|
||||
#include <system/system.h>
|
||||
#include <sysdefs/ioctl.h>
|
||||
#include <sysdefs/ipcpipe.h>
|
||||
#include <sysdefs/processctl.h>
|
||||
#include <uprintf.h>
|
||||
#include <ansiq/all.h>
|
||||
#include <string/char.h>
|
||||
#include <util/util.h>
|
||||
|
||||
void main(void) {
|
||||
uprintf(ANSIQ_CUR_SET(0, 0));
|
||||
uprintf(ANSIQ_SCR_CLR_ALL);
|
||||
|
||||
int32_t ioh = ioctl(IOCTL_NOHANDLE,
|
||||
IOCTL_OPENF,
|
||||
(uint64_t)"temp:/hello.txt",
|
||||
IOCTL_F_WRITE | IOCTL_F_READ | IOCTL_F_MAKE,
|
||||
0
|
||||
);
|
||||
|
||||
char *text = "Hello from FS";
|
||||
ioctl(ioh, IOCTL_WRITE, (uint64_t)text, string_len(text), 0);
|
||||
|
||||
char buf[0x100] = {0};
|
||||
ioctl(ioh, IOCTL_READ, (uint64_t)buf, string_len(text), 0);
|
||||
|
||||
uprintf("file contents: %s\n", buf);
|
||||
|
||||
ioctl(ioh, IOCTL_CLOSEF, 0, 0, 0);
|
||||
|
||||
uprintf("Hello world using uprintf\n");
|
||||
|
||||
const char *tbargs[] = { "-i" };
|
||||
int32_t tb = processctl(-1, PCTL_SPAWN, (uint64_t)"base:/bin/tb", (uint64_t)tbargs, 1);
|
||||
char *tbargs[] = { "-m", "runfile", "-f", "base:/scripts/init.tb" };
|
||||
int32_t tb = processctl(-1, PCTL_SPAWN, (uint64_t)"base:/bin/tb", (uint64_t)&tbargs, ARRLEN(tbargs));
|
||||
uint64_t selfpid = (uint64_t)processctl(-1, PCTL_GETPID, 0, 0, 0);
|
||||
ipcpipe(tb, IPCPIPE_OUT, IPCPIPE_REPLACE, (uint8_t *)selfpid, IPCPIPE_OUT);
|
||||
processctl(tb, PCTL_RUN, 0, 0, 0);
|
||||
while(processctl(tb, PCTL_POLLSTATE, 0, 0, 0) != 2);
|
||||
|
||||
if (ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_MAKE, NULL, 0) < 0) {
|
||||
uprintf("failed to create 10th pipe\n");
|
||||
}
|
||||
ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_MAKE, NULL, 0);
|
||||
ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_ADD_BCAST, NULL, 1);
|
||||
|
||||
int32_t r = ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_ADD_BCAST, NULL, 1);
|
||||
uprintf("%d\n", r);
|
||||
|
||||
while(1) {
|
||||
while(processctl(tb, PCTL_POLLSTATE, 0, 0, 0) != 2) {
|
||||
int32_t kbchr;
|
||||
int32_t read = ipcpipe(IPCPIPE_SELFPID, 10, IPCPIPE_READ, (uint8_t *)&kbchr, sizeof(kbchr));
|
||||
if (read > 0) {
|
||||
kbchr &= 0xFF;
|
||||
if ((kbchr >= 0x20 && kbchr <= 0x7F) || kbchr == 0xA) {
|
||||
uprintf("%c", kbchr & 0xFF);
|
||||
uint8_t c = kbchr & 0xff;
|
||||
if (string_chr_isascii(c)) {
|
||||
if (c != 0) {
|
||||
ipcpipe(tb, IPCPIPE_IN, IPCPIPE_WRITE, &c, 1);
|
||||
}
|
||||
if (string_chr_isprint(c)) {
|
||||
uprintf("%c", c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(;;);
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,92 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <uprintf.h>
|
||||
#include <dlmalloc/malloc.h>
|
||||
#include <args.h>
|
||||
#include <args/args.h>
|
||||
#include <string/string.h>
|
||||
#include <string/char.h>
|
||||
#include <log.h>
|
||||
#include <ansiq/all.h>
|
||||
#include <system/system.h>
|
||||
#include <sysdefs/ipcpipe.h>
|
||||
#include <sysdefs/ioctl.h>
|
||||
#include <sysdefs/processctl.h>
|
||||
#include <errors.h>
|
||||
|
||||
void main(void) {
|
||||
uprintf("Hello from tb!\n");
|
||||
struct {
|
||||
char *modestr;
|
||||
enum { MODE_INTERACTIVE = 1, MODE_RUNFILE = 2 } mode;
|
||||
char *filepath;
|
||||
} CONFIG;
|
||||
|
||||
for (size_t i = 0; i < argslen(); i++) {
|
||||
uprintf("i = %d\n", i);
|
||||
uprintf("arg: %s\n", args()[i]);
|
||||
#define LINEBUF_MAX 1024
|
||||
|
||||
static Arg ARGS[] = {
|
||||
ARG("-m", ARG_STRING, &CONFIG.modestr),
|
||||
ARG("-f", ARG_STRING, &CONFIG.filepath),
|
||||
ARG_END(),
|
||||
};
|
||||
|
||||
void set_config(void) {
|
||||
int32_t ret;
|
||||
if ((ret = parse_args(args(), argslen(), ARGS)) < 0) {
|
||||
uprintf("Could not parse args: %d\n", ret);
|
||||
}
|
||||
|
||||
if (CONFIG.modestr != NULL) {
|
||||
if (string_strcmp(CONFIG.modestr, "interactive") == 0) {
|
||||
CONFIG.mode = MODE_INTERACTIVE;
|
||||
} else if (string_strcmp(CONFIG.modestr, "runfile") == 0) {
|
||||
CONFIG.mode = MODE_RUNFILE;
|
||||
} else {
|
||||
LOG(LOG_ERR, "Unknown mode %s\n", CONFIG.modestr);
|
||||
}
|
||||
} else {
|
||||
CONFIG.mode = MODE_RUNFILE;
|
||||
}
|
||||
}
|
||||
|
||||
void process_cmd(char *cmdtext) {
|
||||
|
||||
}
|
||||
|
||||
void do_file(char *filepath) {
|
||||
int32_t ioh = ioctl(IOCTL_NOHANDLE, IOCTL_OPENF, (uint64_t)filepath, IOCTL_F_READ, 0);
|
||||
if (ioh < 0) {
|
||||
LOG(LOG_ERR, "Could not open %s: %s\n", filepath, ERRSTRING(ioh));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void do_mode_interactive(void) {
|
||||
char linebuf[LINEBUF_MAX];
|
||||
size_t cursor;
|
||||
for(;;) {
|
||||
uprintf("tb# ");
|
||||
|
||||
cursor = 0;
|
||||
string_memset(linebuf, 0, LINEBUF_MAX);
|
||||
char c = 0;
|
||||
while (c != '\n') {
|
||||
int32_t rd = ipcpipe(IPCPIPE_SELFPID, IPCPIPE_IN, IPCPIPE_READ, (uint8_t *)&c, 1);
|
||||
if (rd > 0 && cursor < LINEBUF_MAX) {
|
||||
linebuf[cursor++] = c;
|
||||
}
|
||||
}
|
||||
linebuf[cursor - 1] = '\0';
|
||||
uprintf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
void main(void) {
|
||||
set_config();
|
||||
|
||||
if (CONFIG.mode == MODE_INTERACTIVE) {
|
||||
do_mode_interactive();
|
||||
} else if (CONFIG.mode == MODE_RUNFILE) {
|
||||
if (CONFIG.filepath == NULL) {
|
||||
uprintf("No file provided\n");
|
||||
return;
|
||||
}
|
||||
do_file(CONFIG.filepath);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user