diff options
33 files changed, 1324 insertions, 43 deletions
@@ -45,6 +45,8 @@ such programs include: - ``sleep`` - Sleep for a number of seconds - ``kmsg`` - Read the kernel message buffer - ``readcore`` - Read coredump files +- ``oasm`` - OSMORA [OSMX64](https://github.com/sigsegv7/OSMX64) Assembler +- ``oemu`` - OSMORA [OSMX64](https://github.com/sigsegv7/OSMX64) Emulator And more! See ``usr.bin/*`` diff --git a/etc/motd b/etc/motd new file mode 100644 index 0000000..62718f4 --- /dev/null +++ b/etc/motd @@ -0,0 +1,4 @@ +::::::::::::::::::::::::::::::::::::::: +:: OSMORA GATEWAY ~ Every key echos :: +:: ..... Proceed with purpose ..... :: +::::::::::::::::::::::::::::::::::::::: diff --git a/etc/oemu/test-00.s b/etc/oemu/test-00.s index b34c099..ccd7da1 100644 --- a/etc/oemu/test-00.s +++ b/etc/oemu/test-00.s @@ -1,13 +1,16 @@ -mov x0, #1 ! ~ 0x00000000 -mov x1, #2 ! ~ 0x00000004 -mov x2, #3 ! ~ 0x00000008 - ! X -mov x2, #20 ! ~ 0x0000000C -br x2 ! ~ 0x00000010 -----+ -!!!!!!!!!!!!!! X | -!!!!!!!!!!!!!! X | - ! X | -dec x0 ! ~ 0x00000014 <----+ -inc x1 ! ~ 0x00000018 -add x2, #3 ! ~ 0x0000001C -hlt ! ~ 0x00000020 +mov x0, #1 ! ~ 0x00000000 +mov x1, #2 ! ~ 0x00000004 +mov x2, #3 ! ~ 0x00000008 + ! X +some_label: ! X + mov x2, #20 ! ~ 0x0000000C + br x2 ! ~ 0x00000010 -----+ + !!!!!!!!!!!!!! X | + !!!!!!!!!!!!!! X | + ! X | + dec x0 ! ~ 0x00000014 <----+ + inc x1 ! ~ 0x00000018 + add x2, #3 ! ~ 0x0000001C + mrow x4, #0 ! ~ 0x00000020 + nop ! ~ 0x00000024 + hlt ! ~ 0x00000028 diff --git a/lib/libc/src/stdio/vsnprintf.c b/lib/libc/src/stdio/vsnprintf.c index 24b2df6..0e3d268 100644 --- a/lib/libc/src/stdio/vsnprintf.c +++ b/lib/libc/src/stdio/vsnprintf.c @@ -84,6 +84,9 @@ vsnprintf(char *s, size_t size, const char *fmt, va_list ap) c = *fmt++; switch (c) { + case '%': + printc(s, size, &off, c); + break; case 'c': c1 = (char )__va_arg(ap, int); printc(s, size, &off, c1); diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c index efd1af8..19dcf44 100644 --- a/sys/arch/amd64/amd64/machdep.c +++ b/sys/arch/amd64/amd64/machdep.c @@ -418,6 +418,7 @@ cpu_startup(struct cpu_info *ci) setup_vectors(ci); try_mitigate_spectre(); + ci->online = 1; cpu_get_info(ci); cpu_enable_smep(); diff --git a/sys/dev/cons/cons.c b/sys/dev/cons/cons.c index 88cbcbe..d9d727a 100644 --- a/sys/dev/cons/cons.c +++ b/sys/dev/cons/cons.c @@ -356,6 +356,7 @@ dev_write(dev_t dev, struct sio_txn *sio, int flags) static int dev_read(dev_t dev, struct sio_txn *sio, int flags) { + struct cons_screen *scr = &g_root_scr; struct cons_input input; uint8_t *p; int retval; @@ -369,7 +370,7 @@ dev_read(dev_t dev, struct sio_txn *sio, int flags) return -EFAULT; } - retval = cons_ibuf_pop(&g_root_scr, &input); + retval = cons_ibuf_pop(scr, &input); if (retval < 0) { return -EAGAIN; } @@ -386,7 +387,7 @@ dev_read(dev_t dev, struct sio_txn *sio, int flags) n -= 2; /* Try to get the next byte */ - retval = cons_ibuf_pop(&g_root_scr, &input); + retval = cons_ibuf_pop(scr, &input); if (retval < 0) { break; } diff --git a/sys/include/arch/amd64/cdefs.h b/sys/include/arch/amd64/cdefs.h index 0a20324..d038a15 100644 --- a/sys/include/arch/amd64/cdefs.h +++ b/sys/include/arch/amd64/cdefs.h @@ -31,7 +31,7 @@ #define _AMD64_CDEFS_H_ #include <sys/cdefs.h> -#include <machine/sync.h> +#include <machine/cpu.h> /* * Please use CLI wisely, it is a good idea to use @@ -41,7 +41,7 @@ #define md_pause() __ASMV("rep; nop") /* (F3 90) PAUSE */ #define md_intoff() __ASMV("cli") /* Clear interrupts */ #define md_inton() __ASMV("sti") /* Enable interrupts */ -#define md_hlt() __ASMV("hlt") /* Halt the processor */ +#define md_hlt() cpu_halt() /* Halt the processor */ /* * AMD64 specific defines diff --git a/sys/include/arch/amd64/cpu.h b/sys/include/arch/amd64/cpu.h index 046b621..116661b 100644 --- a/sys/include/arch/amd64/cpu.h +++ b/sys/include/arch/amd64/cpu.h @@ -36,6 +36,7 @@ #include <sys/sched.h> #include <sys/spinlock.h> #include <machine/tss.h> +#include <machine/cdefs.h> #define CPU_IRQ(IRQ_N) (BIT((IRQ_N)) & 0xFF) @@ -51,6 +52,7 @@ struct cpu_info { uint8_t family : 4; /* CPU family ID */ uint8_t has_x2apic : 1; uint8_t tlb_shootdown : 1; + uint8_t online : 1; /* CPU online */ uint8_t ipl; size_t lapic_tmr_freq; uint8_t irq_mask; @@ -80,4 +82,18 @@ void mp_bootstrap_aps(struct cpu_info *ci); extern struct cpu_info g_bsp_ci; +__always_inline static inline void +cpu_halt(void) +{ + struct cpu_info *ci = this_cpu(); + + if (ci != NULL) + ci->online = 0; + + __ASMV("hlt"); + + if (ci != NULL) + ci->online = 1; +} + #endif /* !_MACHINE_CPU_H_ */ diff --git a/sys/include/lib/string.h b/sys/include/lib/string.h index c09e6f4..3255ae5 100644 --- a/sys/include/lib/string.h +++ b/sys/include/lib/string.h @@ -35,6 +35,7 @@ size_t strlen(const char *s); char *itoa(int64_t value, char *buf, int base); +char *strdup(const char *s); int vsnprintf(char *s, size_t size, const char *fmt, va_list ap); int snprintf(char *s, size_t size, const char *fmt, ...); diff --git a/sys/include/sys/limits.h b/sys/include/sys/limits.h index f6aed9d..963d113 100644 --- a/sys/include/sys/limits.h +++ b/sys/include/sys/limits.h @@ -36,4 +36,6 @@ #define ARG_MAX 4096 #define CHAR_BIT 8 #define CPU_MAX 256 +#define VSR_MAX_DOMAIN 16 +#define VSR_MAX_CAPSULE 16 #endif /* !_SYS_LIMITS_H_ */ diff --git a/sys/include/sys/proc.h b/sys/include/sys/proc.h index 9cc9238..89fe638 100644 --- a/sys/include/sys/proc.h +++ b/sys/include/sys/proc.h @@ -39,6 +39,8 @@ #include <sys/syscall.h> #include <sys/exec.h> #include <sys/ucred.h> +#include <sys/limits.h> +#include <sys/vsr.h> #include <sys/filedesc.h> #include <sys/signal.h> #include <sys/vnode.h> @@ -88,6 +90,7 @@ struct proc { struct ucred cred; struct ksiginfo *ksig_list[PROC_SIGMAX]; struct filedesc *fds[PROC_MAX_FILEDES]; + struct vsr_domain *vsr_tab[VSR_MAX_DOMAIN]; struct mmap_lgdr *mlgdr; struct vcache *vcache; struct spinlock vcache_lock; diff --git a/sys/include/sys/queue.h b/sys/include/sys/queue.h index e5d607d..92c1ff2 100644 --- a/sys/include/sys/queue.h +++ b/sys/include/sys/queue.h @@ -27,7 +27,12 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#if !defined(_KERNEL) +#include <stdint.h> +#include <stddef.h> +#else #include <sys/types.h> +#endif /* !_KERNEL */ #ifndef _QUEUE_H_ #define _QUEUE_H_ diff --git a/sys/include/sys/sched.h b/sys/include/sys/sched.h index abc2718..7a859b2 100644 --- a/sys/include/sys/sched.h +++ b/sys/include/sys/sched.h @@ -49,11 +49,13 @@ struct sched_cpu { * * @nproc: Number processes running * @ncpu: Number of CPU cores + * @nhlt: Number of halted CPU cores * @quantum_usec: Scheduler quantum (microseconds) */ struct sched_stat { size_t nproc; uint16_t ncpu; + uint16_t nhlt; uint32_t quantum_usec; struct sched_cpu cpus[CPU_MAX]; }; diff --git a/sys/include/sys/signal.h b/sys/include/sys/signal.h index 9fc767d..eaf2d41 100644 --- a/sys/include/sys/signal.h +++ b/sys/include/sys/signal.h @@ -37,6 +37,7 @@ #define SIGFPE 8 /* Floating point exception */ #define SIGKILL 9 /* Kill */ #define SIGSEGV 11 /* Segmentation violation */ +#define SIGTERM 15 /* Terminate gracefully */ typedef uint32_t sigset_t; @@ -80,5 +81,6 @@ int sigismember(const sigset_t *set, int signo); void sigfpe_default(int signo); void sigkill_default(int signo); void sigsegv_default(int signo); +void sigterm_default(int signo); #endif /* _KERNEL */ #endif /* !_SYS_SIGNAL_H_ */ diff --git a/sys/include/sys/termios.h b/sys/include/sys/termios.h index 27339f1..a3ba794 100644 --- a/sys/include/sys/termios.h +++ b/sys/include/sys/termios.h @@ -33,8 +33,33 @@ /* * c_iflag: Input flags */ -#define ISTRIP 0x00000000 -#define ICRNL 0x00000001 +#define ISTRIP 0x00000001 /* Strip char */ +#define ICRNL 0x00000002 /* Map CR to NL */ +#define BRKINT 0x00000004 /* Signal interrupt on break */ +#define IGNBRK 0x00000008 /* Ignore break condition */ +#define IGNCR 0x00000010 /* Ignore CR */ +#define IGNPAR 0x00000020 /* Ignore chars with parity errors */ +#define INCLR 0x00000040 /* Map NL to CR */ +#define INPCK 0x00000080 /* Enable input parity check */ +#define IXANY 0x00000100 /* Enable any char to restart output */ +#define IXOFF 0x00000200 /* Enable start/stop control */ +#define PARMRK 0x00000400 /* Mark parity errors */ + +/* + * c_oflag: Output flags + */ +#define OPOST 0x00000001 /* Post-process output */ +#define ONLCR 0x00000002 /* Map NL to CR-NL on output */ +#define OCRNL 0x00000004 /* Map CR to NL on output */ +#define ONOCR 0x00000008 /* Map CR to output at col 0 */ +#define ONLRET 0x00000010 /* NL performs CR function */ +#define OFILL 0x00000020 /* Use fill chars for delay */ +#define NLDLY 0x00000040 /* Select newline type */ +#define CRDLY 0x00000080 /* Select carriage-return delays */ +#define TABDLY 0x00000100 /* Select horizontal-tab delays */ +#define BSDLY 0x00000200 /* Select backspace delays */ +#define VTDLY 0x00000400 /* Select veritcal tab delays */ +#define FFDLY 0x00000800 /* Select form-feed delays */ #define NCCS 20 diff --git a/sys/include/sys/vsr.h b/sys/include/sys/vsr.h new file mode 100644 index 0000000..88cb659 --- /dev/null +++ b/sys/include/sys/vsr.h @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _SYS_VSR_H_ +#define _SYS_VSR_H_ + +#include <sys/types.h> +#include <sys/queue.h> +#include <sys/param.h> +#include <sys/ucred.h> +#include <sys/limits.h> +#if defined(_KERNEL) +#include <sys/mutex.h> +#endif /* _KERNEL */ + +#define VSR_FILE 0x00000000 /* Represented by file */ + +/* + * Defines the access semantics of whether + * r/w operations should be passed down to the + * global state or soley affecting a per-process + * shallow copy. + */ +typedef uint32_t vsr_mode_t; + +/* + * The Virtual System Resource namespace consists of + * domains containing named "capsules". The domain is + * simply a table indexed by a type value e.g. VSR_FILE + * and a capsule is simply a structure containing global data + * as well as a shallow copy which is controlled locally by the + * process. The capsule also contains various access semantics + * that help the VSR subsystem determine whether the access should + * be passed down globally or virtualized locally within the process. + */ +typedef uint8_t vsr_domain_t; + +/* + * VSR mode bits + */ +#define VSR_GLOB_WRITE BIT(0) /* Writes are global */ +#define VSR_GLOB_READ BIT(1) /* Reads are global */ +#define VSR_GLOB_CRED BIT(2) /* Global for specific creds */ + +#if defined(_KERNEL) + +struct vsr_capsule; + +/* + * VSR capsule operations + * + * @reclaim: Cleanup resources + */ +struct capsule_ops { + int(*reclaim)(struct vsr_capsule *cap, int flags); +}; + +/* + * Virtual system resource access + * semantics. + * + * @glob: Global data + * @shallow: Local per process copy + * @mode: VSR mode (see VSR_GLOB_*) + * @cred: Creds (used if VSR_GLOBAL_CRED set) + */ +struct vsr_access { + void *glob; + void *shallow; + vsr_mode_t mode; + struct ucred cred; +}; + +/* + * A virtual system resource capsule containing + * resource owner specific data and hashmap + * buckets. + * + * @name: Capsule name (e.g., "consfeat"), must be freed + * @data: Owner specific data + * @shadow: Local shadow copy (per-process) + * @buckets: Hashmap buckets + * @link: Bucket link + * @ops: Capsule operations + * @lock: Mutex lock protecting fields + */ +struct vsr_capsule { + char *name; + void *data; + void *shadow; + TAILQ_HEAD(, vsr_capsule) buckets; + TAILQ_ENTRY(vsr_capsule) link; + struct capsule_ops ops; + struct mutex lock; +}; + +/* + * Virtual system resource table containg + * VSRs for various types. + * + * Each VSR table belongs to a VSR domain + * (e.g., VSR_FILE). + * + * @ncaps: Number of capsules + * @is_init: Set if hashmap is set up + * @capsules: VSR capsule hashmap + */ +struct vsr_table { + struct vsr_capsule *capsules[VSR_MAX_CAPSULE]; +}; + +/* + * Virtual system resource domain (VSR). + * + * A VSR is represented by a specific VSR type + * (see VSR_*). Each VSR has a table of VSR capsules + * looked up by a VSR capsule name. + * + * One per process. + * + * @type: VSR type + * @table: VSR table + */ +struct vsr_domain { + int type; + struct vsr_table table; +}; + +void vsr_init_domains(void); +void vsr_destroy_domains(void); + +struct vsr_domain *vsr_new_domain(vsr_domain_t type); +struct vsr_capsule *vsr_new_capsule(vsr_domain_t type, const char *name); +struct vsr_capsule *vsr_lookup_capsule(vsr_domain_t type, const char *name); + +#endif /* _KERNEL */ +#endif /* !_SYS_VSR_H_ */ diff --git a/sys/kern/kern_accnt.c b/sys/kern/kern_accnt.c index cd15863..51905e7 100644 --- a/sys/kern/kern_accnt.c +++ b/sys/kern/kern_accnt.c @@ -58,6 +58,25 @@ ctl_stat_read(struct ctlfs_dev *cdp, struct sio_txn *sio) return sio->len; } +static uint16_t +cpu_nhlt(void) +{ + uint16_t nhlt = 0; + struct cpu_info *ci; + + for (size_t i = 0; i < CPU_MAX; ++i) { + ci = cpu_get(i); + if (ci == NULL) { + continue; + } + if (!ci->online) { + ++nhlt; + } + } + + return nhlt; +} + /* * Get scheduler accounting information * @@ -71,6 +90,7 @@ sched_stat(struct sched_stat *statp) statp->nproc = atomic_load_64(&g_nthreads); statp->ncpu = cpu_count(); statp->quantum_usec = DEFAULT_TIMESLICE_USEC; + statp->nhlt = cpu_nhlt(); /* * Setup the per-cpu info/statistics diff --git a/sys/kern/kern_sig.c b/sys/kern/kern_sig.c index 58bd52d..044de7b 100644 --- a/sys/kern/kern_sig.c +++ b/sys/kern/kern_sig.c @@ -58,6 +58,12 @@ static struct sigaction sa_tab[] = { .sa_flags = 0, .sa_sigaction = NULL }, + [SIGTERM] = { + .sa_handler = sigterm_default, + .sa_mask = 0, + .sa_flags = 0, + .sa_sigaction = NULL + } }; /* diff --git a/sys/kern/kern_stub.c b/sys/kern/kern_stub.c index 17c6e54..a9a56ac 100644 --- a/sys/kern/kern_stub.c +++ b/sys/kern/kern_stub.c @@ -70,6 +70,18 @@ sigsegv_default(int signo) exit1(td, 0); } +void +sigterm_default(int signo) +{ + static struct proc *td; + + td = this_td(); + syslog_silence(false); + kprintf(OMIT_TIMESTAMP "Terminated (pid=%d)\n", td->pid); + syslog_silence(true); + exit1(td, 0); +} + int dev_noread(void) { diff --git a/sys/kern/kern_vsr.c b/sys/kern/kern_vsr.c new file mode 100644 index 0000000..8fb7fc7 --- /dev/null +++ b/sys/kern/kern_vsr.c @@ -0,0 +1,360 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/vsr.h> +#include <sys/proc.h> +#include <sys/param.h> +#include <sys/limits.h> +#include <sys/syslog.h> +#include <vm/dynalloc.h> +#include <string.h> + +#define pr_trace(fmt, ...) kprintf("vsr: " fmt, ##__VA_ARGS__) +#define pr_error(...) pr_trace(__VA_ARGS__) + +static uint32_t +fnv1_hash(const char *s) +{ + uint32_t hash = 2166136261UL; + const uint8_t *p = (uint8_t *)s; + + while (*p != '\0') { + hash ^= *p; + hash = hash * 0x01000193; + ++p; + } + + return hash; +} + +/* + * Add a VSR capsule to a domain. + */ +static void +vsr_domain_add(struct vsr_domain *vsp, struct vsr_capsule *cap) +{ + struct vsr_table *tab; + struct vsr_capsule **slot; + uint32_t hash; + + if (vsp == NULL || cap == NULL) { + return; + } + + if (cap->name == NULL) { + pr_error("vsr_domain_add: cap->name == NULL\n"); + return; + } + + tab = &vsp->table; + hash = fnv1_hash(cap->name); + slot = &tab->capsules[hash % VSR_MAX_CAPSULE]; + + /* If this slot is free, set it */ + if (*slot == NULL) { + *slot = cap; + return; + } + + /* Handle collision */ + TAILQ_INSERT_TAIL(&(*slot)->buckets, cap, link); +} + +/* + * Handle VSR domain hashmap collisions. + * + * @slot: Slot that we have collided with + * @name: Name to lookup + * + * Returns the pointer to the actual capsule if the + * collision has been resolved, otherwise, NULL if the + * entry to look up was not found. + */ +static struct vsr_capsule * +vsr_domain_clash(struct vsr_capsule *slot, const char *name) +{ + struct vsr_capsule *cap_ent; + + TAILQ_FOREACH(cap_ent, &slot->buckets, link) { + if (cap_ent == NULL) { + continue; + } + + if (strcmp(cap_ent->name, name) == 0) { + return cap_ent; + } + } + + return NULL; +} + +/* + * Lookup a capsule within a VSR domain + * by name. + * + * @vsp: Domain to lookup within + * @name: Name to use as lookup key + * + * Returns NULL if no entry was found. + */ +static struct vsr_capsule * +vfs_domain_lookup(struct vsr_domain *vsp, const char *name) +{ + uint32_t hash; + struct vsr_table *tab; + struct vsr_capsule **slot; + + if (vsp == NULL || name == NULL) { + return NULL; + } + + tab = &vsp->table; + hash = fnv1_hash(name); + slot = &tab->capsules[hash % VSR_MAX_CAPSULE]; + + if (*slot == NULL) { + return NULL; + } + + if (strcmp((*slot)->name, name) != 0) { + return vsr_domain_clash(*slot, name); + } + + return *slot; +} + +/* + * Destroy a VSR capsule + * + * @capule: Capsule to destroy + */ +static void +vsr_destroy_capsule(struct vsr_capsule *capsule) +{ + struct vsr_capsule *bucket; + struct capsule_ops *ops; + + if (capsule->name != NULL) { + dynfree(capsule->name); + capsule->name = NULL; + } + + ops = &capsule->ops; + if (ops->reclaim != NULL) { + ops->reclaim(capsule, 0); + } + + TAILQ_FOREACH(bucket, &capsule->buckets, link) { + if (bucket == NULL) { + continue; + } + vsr_destroy_capsule(bucket); + } + + /* Release any held locks */ + mutex_release(&capsule->lock); +} + +/* + * Destroy a VSR table + * + * @tab: Table to destroy. + */ +static void +vsr_destroy_table(struct vsr_table *tab) +{ + struct vsr_capsule *capsule; + + if (tab == NULL) { + pr_error("vsr_destroy_table: tab is NULL\n"); + return; + } + + for (int i = 0; i < VSR_MAX_CAPSULE; ++i) { + if ((capsule = tab->capsules[i]) == NULL) { + continue; + } + + vsr_destroy_capsule(capsule); + } +} + +/* + * Allocate a new VSR capsule and add it to + * VSR domain. + */ +struct vsr_capsule * +vsr_new_capsule(vsr_domain_t type, const char *name) +{ + struct vsr_capsule *capsule; + struct vsr_domain *domain; + struct proc *td = this_td(); + + /* Valid type? */ + if (type >= VSR_MAX_DOMAIN) { + return NULL; + } + + if (__unlikely(td == NULL)) { + return NULL; + } + + /* + * The VSR domain must be registered for + * us to add any capsules to it. + */ + if ((domain = td->vsr_tab[type]) == NULL) { + pr_error("VSR domain %d not registered\n", type); + return NULL; + } + + /* Allocate a new capsule */ + capsule = dynalloc(sizeof(*capsule)); + if (capsule == NULL) { + return NULL; + } + + memset(capsule, 0, sizeof(*capsule)); + capsule->name = strdup(name); + + TAILQ_INIT(&capsule->buckets); + vsr_domain_add(domain, capsule); + return capsule; +} + +/* + * Allocate a new VSR domain and add it to + * the current process. + * + * @type: VSR type (e.g., VSR_FILE) + */ +struct vsr_domain * +vsr_new_domain(vsr_domain_t type) +{ + struct vsr_domain *domain; + struct vsr_table *tablep; + struct proc *td = this_td(); + + /* Valid type? */ + if (type >= VSR_MAX_DOMAIN) { + return NULL; + } + + /* + * The scheduler should be set up before any + * calls to vsr_new_vec() should be made. + */ + if (__unlikely(td == NULL)) { + return NULL; + } + + /* + * Do not overwrite the entry if it is + * already allocated and log this anomalous + * activity. + */ + if (td->vsr_tab[type] != NULL) { + pr_error("[security]: type %d already allocated\n", type); + return NULL; + } + + domain = dynalloc(sizeof(*domain)); + if (domain == NULL) { + return NULL; + } + + /* Initialize the domain */ + memset(domain, 0, sizeof(*domain)); + domain->type = type; + + /* Initialize the domain's table */ + tablep = &domain->table; + td->vsr_tab[type] = domain; + return domain; +} + +/* + * Lookup a capsule by name for the current + * process. + */ +struct vsr_capsule * +vsr_lookup_capsule(vsr_domain_t type, const char *name) +{ + struct vsr_domain *domain; + struct proc *td = this_td(); + + /* Must be on a process */ + if (__unlikely(td == NULL)) { + return NULL; + } + + /* + * The VSR domain must be registered for + * us to lookup any capsules from it. + */ + if ((domain = td->vsr_tab[type]) == NULL) { + pr_error("VSR domain %d not registered\n", type); + return NULL; + } + + return vfs_domain_lookup(domain, name); +} + +/* + * Initialize per-process domains + */ +void +vsr_init_domains(void) +{ + if (vsr_new_domain(VSR_FILE) == NULL) { + pr_error("failed to initialize VSR file domain\n"); + } +} + +/* + * Destroy per-process domains + */ +void +vsr_destroy_domains(void) +{ + struct proc *td = this_td(); + struct vsr_domain *domain; + + if (__unlikely(td == NULL)) { + return; + } + + for (int i = 0; i < VSR_MAX_DOMAIN; ++i) { + if ((domain = td->vsr_tab[i]) == NULL) { + continue; + } + + vsr_destroy_table(&domain->table); + } +} diff --git a/sys/lib/string/strdup.c b/sys/lib/string/strdup.c new file mode 100644 index 0000000..9c101bc --- /dev/null +++ b/sys/lib/string/strdup.c @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <string.h> +#include <vm/dynalloc.h> + +char * +strdup(const char *s) +{ + size_t s_len; + char *p; + + /* Make sure size is not zero */ + if ((s_len = strlen(s)) == 0) { + return NULL; + } + + /* Allocate new memory for this string */ + p = dynalloc(s_len + 1); + if (p == NULL) { + return NULL; + } + + memcpy(p, s, s_len); + return p; +} diff --git a/usr.bin/kstat/kstat.c b/usr.bin/kstat/kstat.c index 853bc8b..e3b7e6a 100644 --- a/usr.bin/kstat/kstat.c +++ b/usr.bin/kstat/kstat.c @@ -37,6 +37,8 @@ get_sched_stat(void) { struct sched_stat stat; struct sched_cpu *cpu; + double nonline, noffline; + uint16_t online_percent; int fd; fd = open("/ctl/sched/stat", O_RDONLY); @@ -50,10 +52,15 @@ get_sched_stat(void) } close(fd); + noffline = stat.nhlt; + nonline = (stat.ncpu - noffline); + online_percent = (uint16_t)(((double)nonline / (nonline + noffline)) * 100); + printf("-------------------------------\n"); printf("Number of tasks: %d\n", stat.nproc); printf("Number of cores online: %d\n", stat.ncpu); printf("Scheduler quantum: %d usec\n", stat.quantum_usec); + printf("CPU is %d%% online\n", online_percent); printf("-------------------------------\n"); /* diff --git a/usr.bin/oasm/emit.c b/usr.bin/oasm/emit.c index eb38779..6b17eab 100644 --- a/usr.bin/oasm/emit.c +++ b/usr.bin/oasm/emit.c @@ -296,6 +296,87 @@ emit_encode_br(struct emit_state *state, struct oasm_token *tok) return TAILQ_NEXT(tok, link); } +/* + * Encode the MRO type instructions + * + * mrob x1[7:0] + * mrow x1[15:0] ! Mrowwww :3333 + * mrod x1[31:0] + * mroq x[63:0] + * + * Returns the next token on success, + * otherwise NULL. + */ +static struct oasm_token * +emit_encode_mro(struct emit_state *state, struct oasm_token *tok) +{ + inst_t curinst; + reg_t rd; + uint8_t opcode = OSMX64_MROB; + char *inst_str = "mrob"; + + switch (tok->type) { + case TT_MROW: + opcode = OSMX64_MROW; + inst_str = "mrow"; + break; + case TT_MROD: + opcode = OSMX64_MROD; + inst_str = "mrod"; + break; + case TT_MROQ: + opcode = OSMX64_MROQ; + inst_str = "mroq"; + break; + } + + /* Next token should be a register */ + tok = TAILQ_NEXT(tok, link); + if (!tok_is_xreg(tok->type)) { + oasm_err("[emit error]: expected register in '%s'\n", inst_str); + return NULL; + } + + rd = ir_to_reg(tok->type); + if (rd == OSMX64_R_BAD) { + oasm_err("[emit error]: got bad register in '%s'\n", inst_str); + return NULL; + } + + /* Next token should be an IMM */ + tok = TAILQ_NEXT(tok, link); + if (tok->type != TT_IMM) { + oasm_err("[emit error]: expected <imm> after reg in '%s'\n", inst_str); + return NULL; + } + + curinst.opcode = opcode; + curinst.rd = rd; + curinst.imm = tok->imm; + emit_bytes(state, &curinst, sizeof(curinst)); + return TAILQ_NEXT(tok, link); +} + +/* + * Encode a NOP instruction + * + * 'nop' - no operands + * + * Returns the next token on success, + * otherwise NULL. + */ +static struct oasm_token * +emit_encode_nop(struct emit_state *state, struct oasm_token *tok) +{ + inst_t curinst; + + curinst.opcode = OSMX64_NOP; + curinst.rd = 0; + curinst.unused = 0; + emit_bytes(state, &curinst, sizeof(curinst)); + return TAILQ_NEXT(tok, link); +} + int emit_osmx64(struct emit_state *state, struct oasm_token *tp) { @@ -367,6 +448,9 @@ emit_process(struct oasm_state *oasm, struct emit_state *emit) curtok = TAILQ_FIRST(&emit->ir); while (curtok != NULL) { switch (curtok->type) { + case TT_NOP: + curtok = emit_encode_nop(emit, curtok); + break; case TT_MOV: curtok = emit_encode_mov(emit, curtok); break; @@ -387,6 +471,10 @@ emit_process(struct oasm_state *oasm, struct emit_state *emit) curtok = emit_encode_hlt(emit, curtok); break; default: + if (tok_is_mro(curtok->type)) { + curtok = emit_encode_mro(emit, curtok); + break; + } curtok = TAILQ_NEXT(curtok, link); break; } diff --git a/usr.bin/oasm/include/oasm/emit.h b/usr.bin/oasm/include/oasm/emit.h index dab63d0..3fc2674 100644 --- a/usr.bin/oasm/include/oasm/emit.h +++ b/usr.bin/oasm/include/oasm/emit.h @@ -56,12 +56,16 @@ #define OSMX64_OR 0x07 /* Bitwise OR operation */ #define OSMX64_XOR 0x08 /* Bitwise XOR operation */ #define OSMX64_AND 0x09 /* Bitwise AND operation */ -#define OSMX64_NOT 0x10 /* Bitwise NOT operation */ -#define OSMX64_SLL 0x11 /* Shift left logical operation */ -#define OSMX64_SRL 0x12 /* Shift right logical operation */ -#define OSMX64_MOV_IMM 0x13 /* Data move operation from IMM */ -#define OSMX64_HLT 0x14 /* Halt the processor */ -#define OSMX64_BR 0x15 /* Branch */ +#define OSMX64_NOT 0x0A /* Bitwise NOT operation */ +#define OSMX64_SLL 0x0B /* Shift left logical operation */ +#define OSMX64_SRL 0x0C /* Shift right logical operation */ +#define OSMX64_MOV_IMM 0x0D /* Data move operation from IMM */ +#define OSMX64_HLT 0x0E /* Halt the processor */ +#define OSMX64_BR 0x0F /* Branch */ +#define OSMX64_MROB 0x10 /* Mask register over byte */ +#define OSMX64_MROW 0x11 /* Mask register over word */ +#define OSMX64_MROD 0x12 /* Mask register over dword */ +#define OSMX64_MROQ 0x13 /* Mask register over qword */ /* * OSMX64 register definitions diff --git a/usr.bin/oasm/include/oasm/label.h b/usr.bin/oasm/include/oasm/label.h new file mode 100644 index 0000000..8acb369 --- /dev/null +++ b/usr.bin/oasm/include/oasm/label.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _OASM_LABEL_H_ +#define _OASM_LABEL_H_ + +#include <sys/types.h> +#include <sys/queue.h> + +#define MAX_LABELS 128 + +/* + * Represents a label + * + * @name: Label name (e.g., _start) + * @ip: Address at which label code starts + */ +struct oasm_label { + char *name; + uintptr_t ip; + TAILQ_ENTRY(oasm_label) link; + TAILQ_HEAD(, oasm_label) buckets; +}; + +void labels_destroy(void); +int label_enter(const char *name, uintptr_t ip); +struct oasm_label *label_lookup(const char *name); + +#endif /* !_OASM_LABEL_H_ */ diff --git a/usr.bin/oasm/include/oasm/lex.h b/usr.bin/oasm/include/oasm/lex.h index 62183e0..873f6b9 100644 --- a/usr.bin/oasm/include/oasm/lex.h +++ b/usr.bin/oasm/include/oasm/lex.h @@ -33,6 +33,7 @@ #include <sys/queue.h> #include <sys/cdefs.h> #include <stdint.h> +#include <stdbool.h> struct oasm_state; @@ -89,6 +90,7 @@ struct oasm_state; */ typedef enum { TT_UNKNOWN, /* Unknown token */ + TT_NOP, /* No operation */ /* Arithmetic instructions */ TT_ADD, /* 'add' */ @@ -97,12 +99,17 @@ typedef enum { TT_DIV, /* 'div' */ TT_HLT, /* 'hlt' */ TT_BR, /* 'br' */ + TT_MROB, /* 'mrob' */ + TT_MROW, /* 'mrow' */ + TT_MROD, /* 'mrod' */ + TT_MROQ, /* 'mroq' */ /* Register ops */ TT_MOV, /* 'mov' */ TT_INC, /* 'inc' */ TT_DEC, /* 'dec' */ TT_IMM, /* #<n> */ + TT_LABEL, /* 'label: ...' */ /* Register sets */ __XN_REGS, /* x0-x15 */ @@ -155,4 +162,22 @@ tok_is_xreg(tt_t tok) return false; } +/* + * Check if a token is of an MRO type + * instruction. Returns true on match. + */ +__always_inline static inline bool +tok_is_mro(tt_t tok) +{ + switch (tok) { + case TT_MROB: + case TT_MROW: + case TT_MROD: + case TT_MROQ: + return true; + } + + return false; +} + #endif /* !_OASM_LEX_H_ */ diff --git a/usr.bin/oasm/include/oasm/state.h b/usr.bin/oasm/include/oasm/state.h index 5f58144..6dd2435 100644 --- a/usr.bin/oasm/include/oasm/state.h +++ b/usr.bin/oasm/include/oasm/state.h @@ -39,6 +39,8 @@ * OASM state: * * @filename: Filname of unit we are parsing + * @pip: Pseudo instruction pointer + * @label_ip: IP at current label start * @in_fd: Input file descriptor * @out_fd: Resulting binary output file descriptor * @line: Current line number @@ -46,6 +48,8 @@ */ struct oasm_state { char *filename; + off_t pip; + off_t label_ip; int in_fd; int out_fd; off_t line; diff --git a/usr.bin/oasm/label.c b/usr.bin/oasm/label.c new file mode 100644 index 0000000..2647bb9 --- /dev/null +++ b/usr.bin/oasm/label.c @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2023-2025 Ian Marco Moffett and the Osmora Team. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of Hyra nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/types.h> +#include <sys/errno.h> +#include <sys/queue.h> +#include <oasm/label.h> +#include <oasm/log.h> +#include <stdlib.h> +#include <string.h> +#include <stddef.h> +#include <stdbool.h> + +static struct oasm_label *labels[MAX_LABELS]; +static size_t label_count = 0; + +static uint32_t +fnv1_hash(const char *s) +{ + uint32_t hash = 2166136261UL; + const uint8_t *p = (uint8_t *)s; + + while (*p != '\0') { + hash ^= *p; + hash = hash * 0x01000193; + ++p; + } + + return hash; +} + +/* + * The label table is a big hashmap containing + * label entries. This function creates and add + * a new label into the table. + * + * @name: Name of the label (e.g., _start) + * @ip: Instruction pointer + */ +int +label_enter(const char *name, uintptr_t ip) +{ + uint32_t hash = fnv1_hash(name); + uint32_t idx = hash % MAX_LABELS; + struct oasm_label *lp, *lp_new; + + if (label_count >= MAX_LABELS) { + oasm_err("[internal error]: too many labels\n"); + return -EIO; + } + + lp_new = malloc(sizeof(*lp_new)); + if (lp_new == NULL) { + oasm_err("[internal error]: out of memory\n"); + return -ENOMEM; + } + + /* Initialize the label */ + lp_new->name = strdup(name); + lp_new->ip = ip; + TAILQ_INIT(&lp_new->buckets); + + /* + * If there is no existing entry here, we + * can take this slot. + */ + lp = labels[idx]; + if (lp == NULL) { + labels[idx] = lp_new; + ++label_count; + return 0; + } + + /* + * To prevent collisions in our table here, + * we must check if the name matches at all. + * If it does not, there is a collision and + * we'll have to add this to a bucket. + */ + if (strcmp(name, lp->name) != 0) { + TAILQ_INSERT_TAIL(&lp->buckets, lp_new, link); + ++label_count; + return 0; + } + + /* Can't put the same entry in twice */ + oasm_err("[internal error]: duplicate labels\n"); + return -EEXIST; +} + +/* + * Find a label entry in the label table. + * + * @name: Name of the label to lookup (e.g., _start) + */ +struct oasm_label * +label_lookup(const char *name) +{ + uint32_t hash = fnv1_hash(name); + uint32_t idx = hash % MAX_LABELS; + struct oasm_label *lp, *lp_tmp; + + lp = labels[idx]; + if (lp == NULL) { + return NULL; + } + + /* Is this the label we are looking up? */ + if (strcmp(name, lp->name) == 0) { + return lp; + } + + /* Maybe there was a collision? */ + TAILQ_FOREACH(lp_tmp, &lp->buckets, link) { + if (strcmp(name, lp_tmp->name) == 0) { + return lp_tmp; + } + } + + return NULL; +} + +/* + * Clean up all allocated labels by + * calling free() on each entry of + * the queue. + */ +void +labels_destroy(void) +{ + struct oasm_label *lp; + + for (size_t i = 0; i < MAX_LABELS; ++i) { + lp = labels[i]; + if (lp != NULL) { + free(lp->name); + free(lp); + } + } +} diff --git a/usr.bin/oasm/lex.c b/usr.bin/oasm/lex.c index a33a570..fea9dc3 100644 --- a/usr.bin/oasm/lex.c +++ b/usr.bin/oasm/lex.c @@ -40,6 +40,7 @@ static char putback = '\0'; /* Instruction mnemonic strings */ +#define S_IMN_NOP "nop" #define S_IMN_MOV "mov" #define S_IMN_ADD "add" #define S_IMN_SUB "sub" @@ -49,6 +50,24 @@ static char putback = '\0'; #define S_IMN_DEC "dec" #define S_IMN_HLT "hlt" #define S_IMN_BR "br" +#define S_IMN_MROB "mrob" +#define S_IMN_MROW "mrow" +#define S_IMN_MROD "mrod" +#define S_IMN_MROQ "mroq" + +/* Instruction length */ +#define OSMX64_INST_LEN 4 + +/* + * Update the state when the caller encounters + * a newline. + */ +static inline void +lex_newline(struct oasm_state *state) +{ + ++state->line; + state->pip += OSMX64_INST_LEN; +} /* * Returns 0 if a char is counted as a @@ -63,7 +82,7 @@ lex_skippable(struct oasm_state *state, char c) case '\t': return 0; case '\r': return 0; case '\n': - ++state->line; + lex_newline(state); return 0; } @@ -154,6 +173,10 @@ lex_nomstr(struct oasm_state *state, char **res) retval = tmp; break; } + if (tmp == ':') { + retval = tmp; + break; + } if (tmp == '\n') { ++state->line; retval = tmp; @@ -205,6 +228,41 @@ token_cfi(char *p) return TT_UNKNOWN; } +/* + * Bitwise MRO instructions + */ +static tt_t +token_bitw_mro(char *p) +{ + if (strcmp(p, S_IMN_MROB) == 0) { + return TT_MROB; + } else if (strcmp(p, S_IMN_MROW) == 0) { + return TT_MROW; + } else if (strcmp(p, S_IMN_MROD) == 0) { + return TT_MROD; + } else if (strcmp(p, S_IMN_MROQ) == 0) { + return TT_MROQ; + } + + return TT_UNKNOWN; +} + +/* + * Bitwise instructions + */ +static tt_t +token_bitw(char *p) +{ + tt_t token; + + token = token_bitw_mro(p); + if (token != TT_UNKNOWN) { + return token; + } + + return TT_UNKNOWN; +} + static tt_t token_xreg(char *p) { @@ -295,7 +353,7 @@ lex_tok(struct oasm_state *state, struct oasm_token *ttp) switch (c) { case '\n': - ++state->line; + lex_newline(state); return 0; case '\0': return -1; @@ -306,7 +364,21 @@ lex_tok(struct oasm_state *state, struct oasm_token *ttp) ttp->raw = NULL; lex_putback(c); - lex_nomstr(state, &p); + c = lex_nomstr(state, &p); + + while (c == ':') { + ttp->type = TT_LABEL; + ttp->raw = p; + state->label_ip = state->pip; + return 0; + } + + /* No operation? */ + if (strcmp(p, S_IMN_NOP) == 0) { + ttp->type = TT_NOP; + ttp->raw = p; + return 0; + } /* Arithmetic operation? */ if ((tok = token_arith(p)) != TT_UNKNOWN) { @@ -330,6 +402,12 @@ lex_tok(struct oasm_state *state, struct oasm_token *ttp) return 0; } + if ((tok = token_bitw(p)) != TT_UNKNOWN) { + ttp->type = tok; + ttp->raw = p; + return 0; + } + /* Immediate operand? */ if ((tok = token_operand(p)) != TT_UNKNOWN) { if (tok == TT_IMM) { @@ -340,6 +418,7 @@ lex_tok(struct oasm_state *state, struct oasm_token *ttp) ttp->raw = p; return 0; } + oasm_err("bad token \"%s\"\n", p); lex_try_free(p); return -1; diff --git a/usr.bin/oasm/parse.c b/usr.bin/oasm/parse.c index 673fbcd..c81b498 100644 --- a/usr.bin/oasm/parse.c +++ b/usr.bin/oasm/parse.c @@ -34,10 +34,12 @@ #include <oasm/lex.h> #include <oasm/parse.h> #include <oasm/log.h> +#include <oasm/label.h> static struct emit_state emit_state; static const char *tokstr[] = { [ TT_UNKNOWN] = "bad", + [ TT_NOP ] = "nop", [ TT_ADD ] = "add", [ TT_SUB ] = "sub", [ TT_MUL ] = "mul", @@ -49,6 +51,14 @@ static const char *tokstr[] = { [ TT_DEC ] = "dec", [ TT_MOV ] = "mov", [ TT_IMM ] = "<imm>", + [ TT_LABEL] = "<label>", + + /* Bitwise */ + [ TT_MROB ] = "mrob", + [ TT_MROW ] = "mrow", + [ TT_MROD ] = "mrod", + [ TT_MROQ ] = "mroq", + /* X<n> registers */ [ TT_X0 ] = "x0", @@ -117,8 +127,12 @@ parse_reg(struct oasm_state *state, struct oasm_token *tok) state->last = tok->type; break; default: + if (tok_is_mro(state->last)) { + break; + } + p = tokstr[state->last]; - oasm_err("bad instruction '%s' for regop\n", p); + oasm_err("bad token '%s' for regop\n", p); return -1; } @@ -134,22 +148,24 @@ parse_reg(struct oasm_state *state, struct oasm_token *tok) } static int -parse_imm(struct oasm_token *tok, tt_t last) -{ - return 0; -} - -static int parse_tok(struct oasm_state *state, struct oasm_token *tok) { const char *p; int error; switch (tok->type) { + case TT_NOP: + state->last = tok->type; + emit_osmx64(&emit_state, tok); + break; case TT_BR: state->last = tok->type; emit_osmx64(&emit_state, tok); break; + case TT_LABEL: + state->last = tok->type; + label_enter(tok->raw, state->pip); + break; case TT_HLT: state->last = tok->type; emit_osmx64(&emit_state, tok); @@ -188,6 +204,12 @@ parse_tok(struct oasm_state *state, struct oasm_token *tok) emit_osmx64(&emit_state, tok); break; default: + if (tok_is_mro(tok->type)) { + state->last = tok->type; + emit_osmx64(&emit_state, tok); + return 0; + } + if (!tok->is_reg) { oasm_err("syntax error\n"); return -1; @@ -230,4 +252,5 @@ parse_enter(struct oasm_state *state) /* Process then destroy the emit state */ emit_process(state, &emit_state); emit_destroy(&emit_state); + labels_destroy(); } diff --git a/usr.bin/oemu/cpu.c b/usr.bin/oemu/cpu.c index 07ddc7b..49d4671 100644 --- a/usr.bin/oemu/cpu.c +++ b/usr.bin/oemu/cpu.c @@ -27,15 +27,35 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include <sys/types.h> #include <sys/param.h> #include <stdlib.h> #include <string.h> #include <stdio.h> +#include <stdbool.h> #include <oemu/cpu.h> #include <oemu/types.h> #include <oemu/osmx64.h> /* + * Return true if the instruction is an + * MRO type instruction. + */ +static bool +cpu_is_mro(inst_t *inst) +{ + switch (inst->opcode) { + case INST_MROB: + case INST_MROW: + case INST_MROD: + case INST_MROQ: + return true; + } + + return false; +} + +/* * Decode the INST_MOV_IMM instruction * * @cpu: CPU that is executing @@ -225,6 +245,77 @@ cpu_br(struct oemu_cpu *cpu, inst_t *inst) } /* + * Decode MRO type instructions + */ +static void +cpu_mro(struct oemu_cpu *cpu, inst_t *inst) +{ + inst_t *next_inst; + struct cpu_regs *regs = &cpu->regs; + char *inst_str = "bad"; + uint64_t mask = 0; + bool set_mask = false; + imm_t imm; + + switch (inst->imm) { + case 0: break; + case 1: + set_mask = true; + break; + default: + imm = inst->imm & 1; + if (inst->imm == 1) { + set_mask = true; + } + break; + } + + switch (inst->opcode) { + case INST_MROB: + inst_str = "mrob"; + if (!set_mask) { + break; + } + mask |= MASK(8); + break; + case INST_MROW: + inst_str = "mrow"; + if (!set_mask) { + break; + } + mask |= MASK(16); + break; + case INST_MROD: + inst_str = "mrod"; + if (!set_mask) { + break; + } + mask |= MASK(32); + break; + case INST_MROQ: + inst_str = "mroq"; + if (!set_mask) { + break; + } + mask |= __UINT64_MAX; + break; + } + + if (inst->rd > NELEM(regs->xreg)) { + printf("bad register operand for '%s'\n", inst_str); + return; + } + + if (set_mask) { + imm = regs->xreg[inst->rd] |= mask; + printf("set %x->x%d, new=%x\n", mask, inst->rd, imm); + } else { + imm = regs->xreg[inst->rd] &= ~mask; + printf("cleared %x->x%d, new=%x\n", mask, inst->rd, imm); + } +} + +/* * Reset a CPU to a default state */ void @@ -286,6 +377,10 @@ cpu_kick(struct oemu_cpu *cpu, struct sysmem *mem) inst = (inst_t *)&memp[regs->ip]; switch (inst->opcode) { + case INST_NOP: + /* NOP */ + regs->ip += sizeof(*inst); + continue; case INST_MOV_IMM: cpu_mov_imm(cpu, inst); break; @@ -310,8 +405,20 @@ cpu_kick(struct oemu_cpu *cpu, struct sysmem *mem) case INST_BR: cpu_br(cpu, inst); break; + default: + if (cpu_is_mro(inst)) { + cpu_mro(cpu, inst); + } + break; } + /* + * X0 is readonly and should always be zero, undo + * any writes or side effects from any operations + * upon this register. + */ + regs->xreg[0] = 0; + /* Is this a halt instruction? */ if (inst->opcode == INST_HLT) { printf("HALTED\n"); diff --git a/usr.bin/oemu/include/oemu/osmx64.h b/usr.bin/oemu/include/oemu/osmx64.h index b1df5d3..ffd6156 100644 --- a/usr.bin/oemu/include/oemu/osmx64.h +++ b/usr.bin/oemu/include/oemu/osmx64.h @@ -43,12 +43,16 @@ #define INST_OR 0x07 /* Bitwise OR operation */ #define INST_XOR 0x08 /* Bitwise XOR operation */ #define INST_AND 0x09 /* Bitwise AND operation */ -#define INST_NOT 0x10 /* Bitwise NOT operation */ -#define INST_SLL 0x11 /* Shift left logical operation */ -#define INST_SRL 0x12 /* Shift right logical operation */ -#define INST_MOV_IMM 0x13 /* Data move operation from IMM */ -#define INST_HLT 0x14 /* Halt */ -#define INST_BR 0x15 /* Branch */ +#define INST_NOT 0x0A /* Bitwise NOT operation */ +#define INST_SLL 0x0B /* Shift left logical operation */ +#define INST_SRL 0x0C /* Shift right logical operation */ +#define INST_MOV_IMM 0x0D /* Data move operation from IMM */ +#define INST_HLT 0x0E /* Halt */ +#define INST_BR 0x0F /* Branch */ +#define INST_MROB 0x10 /* Mask register over byte */ +#define INST_MROW 0x11 /* Mask register over word */ +#define INST_MROD 0x12 /* Mask register over dword */ +#define INST_MROQ 0x13 /* Mask register over qword */ /* Registers */ #define REG_X0 0x00 diff --git a/usr.bin/osh/osh.c b/usr.bin/osh/osh.c index af7f4ab..545f95a 100644 --- a/usr.bin/osh/osh.c +++ b/usr.bin/osh/osh.c @@ -43,6 +43,9 @@ #define is_printable(C) ((C) >= 32 && (C) <= 126) #define is_ascii(C) ((C) >= 0 && (C) <= 128) +#define INPUT_SIZE 64 + +#define REPEAT "!!" #define COMMENT '@' #define WELCOME \ ":::::::::::::::::::::::::::::::::::::::\n" \ @@ -66,7 +69,8 @@ #define PROMPT "[%s::osmora]~ " -static char buf[64]; +static char last_command[INPUT_SIZE]; +static char buf[INPUT_SIZE]; static int running; static int bell_fd; static bool bs_bell = true; /* Beep on backspace */ @@ -365,6 +369,18 @@ parse_line(char *input) struct parse_state state = {0}; pid_t child; + /* + * If we are using the REPEAT shorthand, + * repeat the last command. We return -EAGAIN + * to indicate we did not parse a normal command + * so the repeat command isn't pushed into the last + * command buffer and we enter a recursive hell. + */ + if (strcmp(input, REPEAT) == 0) { + parse_line(last_command); + return -EAGAIN; + } + /* Ensure the aux vector is zeored */ memset(argv, 0, sizeof(argv)); @@ -426,6 +442,25 @@ open_script(const char *pathname) return 0; } +static void +dump_file(const char *pathname) +{ + FILE *file; + char buf[64]; + int fd; + + file = fopen(pathname, "r"); + if (file == NULL) { + return; + } + + while (fgets(buf, sizeof(buf), file) != NULL) { + printf("%s", buf); + } + + fclose(file); +} + int main(int argc, char **argv) { @@ -442,7 +477,7 @@ main(int argc, char **argv) running = 1; bell_fd = open("/dev/beep", O_WRONLY); - puts(WELCOME); + dump_file("/etc/motd"); while (running) { printf(PROMPT, getlogin()); @@ -457,6 +492,7 @@ main(int argc, char **argv) continue; } + memcpy(last_command, buf, buf_i + 1); buf[0] = '\0'; } return 0; |