diff options
-rw-r--r-- | Makefile.in | 2 | ||||
-rw-r--r-- | sys/arch/aarch64/conf/GENERIC | 7 | ||||
-rw-r--r-- | sys/arch/amd64/conf/GENERIC | 8 | ||||
-rw-r--r-- | sys/conf/BLACKLIST | 1 | ||||
-rw-r--r-- | sys/conf/GENERIC | 9 | ||||
-rw-r--r-- | usr.bin/oasm/Makefile | 7 | ||||
-rw-r--r-- | usr.bin/oasm/include/oasm/lex.h | 121 | ||||
-rw-r--r-- | usr.bin/oasm/include/oasm/log.h | 48 | ||||
-rw-r--r-- | usr.bin/oasm/include/oasm/parse.h | 37 | ||||
-rw-r--r-- | usr.bin/oasm/include/oasm/state.h | 55 | ||||
-rw-r--r-- | usr.bin/oasm/lex.c | 294 | ||||
-rw-r--r-- | usr.bin/oasm/log.c | 81 | ||||
-rw-r--r-- | usr.bin/oasm/oasm.c | 65 | ||||
-rw-r--r-- | usr.bin/oasm/parse.c | 204 | ||||
-rw-r--r-- | usr.bin/osh/osh.c | 103 |
15 files changed, 994 insertions, 48 deletions
diff --git a/Makefile.in b/Makefile.in index 0552761..42a0f94 100644 --- a/Makefile.in +++ b/Makefile.in @@ -15,7 +15,7 @@ override KERNEL_LDFLAGS = -no-pie -nostdlib -znoexecstack -zmax-page-size=0x1000 override QEMU_FLAGS = @QEMU_FLAGS@ override KERNEL_DEFINES = $(KBUILD_ARGS) -DHYRA_VERSION="\"$(HYRA_VERSION)\""\ -DHYRA_BUILDDATE="\"@HYRA_BUILDDATE@\""\ - -DHYRA_ARCH="\"@ARCH@\"" $(shell cat sys/arch/$(ARCH)/conf/GENERIC | tools/kconf/kconf) + -DHYRA_ARCH="\"@ARCH@\"" $(shell cat sys/arch/$(ARCH)/conf/GENERIC sys/conf/GENERIC | tools/kconf/kconf) ###################### # Toolchain ###################### diff --git a/sys/arch/aarch64/conf/GENERIC b/sys/arch/aarch64/conf/GENERIC index eeb9d9d..702a248 100644 --- a/sys/arch/aarch64/conf/GENERIC +++ b/sys/arch/aarch64/conf/GENERIC @@ -1,10 +1,3 @@ // Kernel options option SERIAL_DEBUG yes // Enable kmsg serial logging option USER_KMSG yes // Show kmsg in user consoles - -// Kernel constants -setval SCHED_NQUEUE 4 // Number of scheduler queues (for MLFQ) - -// Console attributes -setval CONSOLE_BG 0x000000 -setval CONSOLE_FG 0xB57614 diff --git a/sys/arch/amd64/conf/GENERIC b/sys/arch/amd64/conf/GENERIC index e407fa9..6f573f3 100644 --- a/sys/arch/amd64/conf/GENERIC +++ b/sys/arch/amd64/conf/GENERIC @@ -9,12 +9,4 @@ option SPECTRE_IBRS no // Enable the IBRS CPU feature option SERIAL_DEBUG yes // Enable kmsg serial logging option USER_KMSG no // Show kmsg in user consoles option CPU_SMEP yes // Supervisor Memory Exec Protection -option PANIC_SCR no // Clear screen on panic option I8042_POLL yes // Use polling for the i8042 - -// Kernel constants -setval SCHED_NQUEUE 4 // Number of scheduler queues (for MLFQ) - -// Console attributes -setval CONSOLE_BG 0x000000 -setval CONSOLE_FG 0xB57614 diff --git a/sys/conf/BLACKLIST b/sys/conf/BLACKLIST new file mode 100644 index 0000000..4eb32d8 --- /dev/null +++ b/sys/conf/BLACKLIST @@ -0,0 +1 @@ +xhci ahci nvme diff --git a/sys/conf/GENERIC b/sys/conf/GENERIC new file mode 100644 index 0000000..5734c43 --- /dev/null +++ b/sys/conf/GENERIC @@ -0,0 +1,9 @@ +// Kernel options +option PANIC_SCR no // Clear screen on panic + +// Kernel constants +setval SCHED_NQUEUE 4 // Number of scheduler queues (for MLFQ) + +// Console attributes +setval CONSOLE_BG 0x000000 +setval CONSOLE_FG 0xB57614 diff --git a/usr.bin/oasm/Makefile b/usr.bin/oasm/Makefile new file mode 100644 index 0000000..a83aaab --- /dev/null +++ b/usr.bin/oasm/Makefile @@ -0,0 +1,7 @@ +include user.mk + +CFILES = $(shell find . -name "*.c") +CFLAGS = -Iinclude/ + +$(ROOT)/base/usr/bin/oasm: + gcc $(CFILES) -o $@ $(INTERNAL_CFLAGS) $(CFLAGS) diff --git a/usr.bin/oasm/include/oasm/lex.h b/usr.bin/oasm/include/oasm/lex.h new file mode 100644 index 0000000..9e293e6 --- /dev/null +++ b/usr.bin/oasm/include/oasm/lex.h @@ -0,0 +1,121 @@ +/* + * 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_LEX_H_ +#define _OASM_LEX_H_ + +#include <stdint.h> + +struct oasm_state; + +#define __XN_REGS \ + TT_X0, \ + TT_X1, \ + TT_X2, \ + TT_X3, \ + TT_X4, \ + TT_X5, \ + TT_X6, \ + TT_X7, \ + TT_X8, \ + TT_X9, \ + TT_X10, \ + TT_X11, \ + TT_X12, \ + TT_X13, \ + TT_X14, \ + TT_X15 + +#define __FN_REGS \ + TT_F0, \ + TT_F1, \ + TT_F2, \ + TT_F3, \ + TT_F4, \ + TT_F5, \ + TT_F6, \ + TT_F7 + +#define __DN_REGS \ + TT_D0, \ + TT_D1, \ + TT_D2, \ + TT_D3, \ + TT_D4, \ + TT_D5, \ + TT_D6, \ + TT_D7 + +#define __VN_REGS \ + TT_V0, \ + TT_V1, \ + TT_V2, \ + TT_V3, \ + TT_V4, \ + TT_V5, \ + TT_V6, \ + TT_V7 + +/* + * Token type definitions + */ +typedef enum { + TT_UNKNOWN, /* Unknown token */ + + /* Arithmetic instructions */ + TT_ADD, /* 'add' */ + TT_SUB, /* 'sub' */ + TT_MUL, /* 'mul' */ + TT_DIV, /* 'div' */ + + /* Register ops */ + TT_MOV, /* 'mov' */ + TT_INC, /* 'inc' */ + TT_DEC, /* 'dec' */ + TT_IMM, /* #<n> */ + + /* Register sets */ + __XN_REGS, /* x0-x15 */ + __FN_REGS, /* f0-f7 */ + __DN_REGS, /* d0-d7 */ + __VN_REGS, /* v0-v7 */ + + /* Symbols */ + TT_COMMA, /* ',' */ +} tt_t; + +struct oasm_token { + tt_t type; + uint8_t is_reg : 1; + char *raw; +}; + +int lex_tok(struct oasm_state *state, struct oasm_token *ttp); + +#endif /* !_OASM_LEX_H_ */ diff --git a/usr.bin/oasm/include/oasm/log.h b/usr.bin/oasm/include/oasm/log.h new file mode 100644 index 0000000..330c273 --- /dev/null +++ b/usr.bin/oasm/include/oasm/log.h @@ -0,0 +1,48 @@ +/* + * 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_LOG_H_ +#define _OASM_LOG_H_ + +#include <sys/types.h> +#include <sys/cdefs.h> +#include <stdio.h> + +#define ERROR_COLOR "\033[31;40m" +#define WARN_COLOR "\033[35;40m" + +void __oasm_debug(const char *fmt, ...); +void __oasm_err(const char *fmt, ...); +void __oasm_warn(const char *fmt, ...); + +#define oasm_debug(...) __oasm_debug(__VA_ARGS__) +#define oasm_err(...) __oasm_err(__VA_ARGS__) +#define oasm_warn(...) __oasm_warn(__VA_ARGS__) + +#endif /* !_OASM_LOG_H_ */ diff --git a/usr.bin/oasm/include/oasm/parse.h b/usr.bin/oasm/include/oasm/parse.h new file mode 100644 index 0000000..04962e7 --- /dev/null +++ b/usr.bin/oasm/include/oasm/parse.h @@ -0,0 +1,37 @@ +/* + * 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_PARSE_H_ +#define _OASM_PARSE_H_ + +#include <oasm/state.h> + +void parse_enter(struct oasm_state *state); + +#endif /* !_OASM_PARSE_H_ */ diff --git a/usr.bin/oasm/include/oasm/state.h b/usr.bin/oasm/include/oasm/state.h new file mode 100644 index 0000000..5f58144 --- /dev/null +++ b/usr.bin/oasm/include/oasm/state.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_STATE_H_ +#define _OASM_STATE_H_ + +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> +#include <oasm/lex.h> + +/* + * OASM state: + * + * @filename: Filname of unit we are parsing + * @in_fd: Input file descriptor + * @out_fd: Resulting binary output file descriptor + * @line: Current line number + * @last: Last token + */ +struct oasm_state { + char *filename; + int in_fd; + int out_fd; + off_t line; + tt_t last; +}; + +#endif /* !_OASM_STATE_H_ */ diff --git a/usr.bin/oasm/lex.c b/usr.bin/oasm/lex.c new file mode 100644 index 0000000..afbe21d --- /dev/null +++ b/usr.bin/oasm/lex.c @@ -0,0 +1,294 @@ +/* + * 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/errno.h> +#include <string.h> +#include <oasm/state.h> +#include <oasm/lex.h> +#include <oasm/log.h> + +#define is_num(c) ((c) >= '0' && (c) <= '9') + +static char putback = '\0'; + +/* Instruction mnemonic strings */ +#define S_IMN_MOV "mov" +#define S_IMN_ADD "add" +#define S_IMN_SUB "sub" +#define S_IMN_DIV "div" +#define S_IMN_INC "inc" +#define S_IMN_DEC "dec" + +/* + * Returns 0 if a char is counted as a + * skippable token. Otherwise, -1 + */ +static inline int +lex_skippable(struct oasm_state *state, char c) +{ + switch (c) { + case ' ': return 0; + case '\f': return 0; + case '\t': return 0; + case '\r': return 0; + case '\n': + ++state->line; + return 0; + } + + return -1; +} + +/* + * Put back a token to grab later + * + * @c: Character to put back + */ +static inline char +lex_putback(char c) +{ + putback = c; + return c; +} + +/* + * Grab a character from the input file + * descriptor. + */ +static char +lex_cin(struct oasm_state *state) +{ + char retval; + + if (putback != '\0') { + retval = putback; + putback = '\0'; + return retval; + } + + if (read(state->in_fd, &retval, 1) <= 0) { + return '\0'; + } + return retval; +} + +/* + * Nom an operation, directive or any kind + * of raw string (unquoted/builtin) and return + * memory allocated by strdup() pointing to the + * string. + * + * @state: OASM state pointer + * @res: Resulting string + * + * Returns 0 on success. Greater than zero + * value of the last character if a comma or + * space was not buffered. + */ +static int +lex_nomstr(struct oasm_state *state, char **res) +{ + char buf[256]; + int retval = 0, n = 0; + int tmp; + + memset(buf, 0, sizeof(buf)); + + /* + * We are filling the buffer containing + * the operation or directive. + * + * Keep going until we hit a space or comman (^) + * Examples of such strings (everything in '[]'): + * + * [mov] [x0], [#1] + * ^ ^ + */ + while ((tmp = lex_cin(state)) != 0) { + if (tmp == ' ' || tmp == ',') { + retval = tmp; + break; + } + if (tmp == '\n') { + ++state->line; + retval = tmp; + break; + + } + + buf[n++] = tmp; + } + + *res = strdup(buf); + return retval; +} + +static tt_t +token_arith(char *p) +{ + if (strcmp(p, S_IMN_MOV) == 0) { + return TT_MOV; + } else if (strcmp(p, S_IMN_INC) == 0) { + return TT_INC; + } else if (strcmp(p, S_IMN_DEC) == 0) { + return TT_DEC; + } else if (strcmp(p, S_IMN_ADD) == 0) { + return TT_ADD; + } else if (strcmp(p, S_IMN_SUB) == 0) { + return TT_SUB; + } else if (strcmp(p, S_IMN_DIV) == 0) { + return TT_DIV; + } + + return TT_UNKNOWN; +} + +static tt_t +token_xreg(char *p) +{ + int num; + + if (p[0] != 'x') { + return TT_UNKNOWN; + } + + if (!is_num(p[1])) { + return TT_UNKNOWN; + } + + num = atoi(&p[1]); + switch (num) { + case 0: return TT_X0; + case 1: return TT_X1; + case 2: return TT_X2; + case 3: return TT_X3; + case 4: return TT_X4; + case 5: return TT_X5; + case 6: return TT_X6; + case 7: return TT_X7; + case 8: return TT_X8; + case 9: return TT_X9; + case 10: return TT_X10; + case 11: return TT_X11; + case 12: return TT_X12; + case 13: return TT_X13; + case 14: return TT_X14; + case 15: return TT_X15; + } + + return TT_UNKNOWN; +} + +static tt_t +token_operand(char *p) +{ + /* Is this a numeric constant? */ + if (p[0] == '#') { + return TT_IMM; + } + + return TT_UNKNOWN; +} + +static tt_t +token_reg(char *p) +{ + tt_t tok; + + if ((tok = token_xreg(p)) != TT_UNKNOWN) { + return tok; + } + + return TT_UNKNOWN; +} + +int +lex_tok(struct oasm_state *state, struct oasm_token *ttp) +{ + char *p; + char c = ' '; + int tmp; + tt_t tok; + + if (state == NULL || ttp == NULL) { + return -EINVAL; + } + + /* + * Grab characters. If they are skippable, + * don't use them. + */ + while (lex_skippable(state, c) == 0) { + if ((c = lex_cin(state)) == 0) { + return -1; + } + } + + switch (c) { + case '\n': + ++state->line; + return 0; + case '\0': + return -1; + case ',': + return TT_COMMA; + default: + ttp->type = TT_UNKNOWN; + ttp->raw = NULL; + + lex_putback(c); + lex_nomstr(state, &p); + + /* Arithmetic operation? */ + if ((tok = token_arith(p)) != TT_UNKNOWN) { + ttp->type = tok; + ttp->raw = p; + return 0; + } + + /* Register? */ + if ((tok = token_reg(p)) != TT_UNKNOWN) { + ttp->is_reg = 1; + ttp->type = tok; + ttp->raw = p; + return 0; + } + + /* Immediate operand? */ + if ((tok = token_operand(p)) != TT_UNKNOWN) { + ttp->type = tok; + ttp->raw = p; + return 0; + } + oasm_err("bad token \"%s\"\n", p); + return -1; + } + + return 0; +} diff --git a/usr.bin/oasm/log.c b/usr.bin/oasm/log.c new file mode 100644 index 0000000..c408865 --- /dev/null +++ b/usr.bin/oasm/log.c @@ -0,0 +1,81 @@ +/* + * 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 <oasm/log.h> +#include <oasm/state.h> +#include <stdarg.h> +#include <stdio.h> + +/* TODO FIXME: Use stdarg.h */ +#define __va_start(ap, fmt) __builtin_va_start(ap, fmt) +#define __va_end(ap) __builtin_va_end(ap) + +extern struct oasm_state g_state; + +void +oasm_debug(const char *fmt, ...) +{ + char buf[512]; + va_list ap; + int ret; + + __va_start(ap, fmt); + ret = vsnprintf(buf, sizeof(buf), fmt, ap); + printf("[debug]: %s\033[0m", buf); + printf("\033[0m"); + __va_end(ap); +} + +void +oasm_err(const char *fmt, ...) +{ + char buf[512]; + va_list ap; + int ret; + + __va_start(ap, fmt); + ret = vsnprintf(buf, sizeof(buf), fmt, ap); + printf(ERROR_COLOR "error: %s\033[0m", buf); + printf("%s: line %d\n", g_state.filename, g_state.line); + __va_end(ap); +} + +void +oasm_warn(const char *fmt, ...) +{ + char buf[512]; + va_list ap; + int ret; + + __va_start(ap, fmt); + ret = vsnprintf(buf, sizeof(buf), fmt, ap); + printf(WARN_COLOR "warning: %s\033[0m", buf); + printf("line %d\n", g_state.filename, g_state.line); + __va_end(ap); +} diff --git a/usr.bin/oasm/oasm.c b/usr.bin/oasm/oasm.c new file mode 100644 index 0000000..844f004 --- /dev/null +++ b/usr.bin/oasm/oasm.c @@ -0,0 +1,65 @@ +/* + * 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 <stdio.h> +#include <fcntl.h> +#include <unistd.h> +#include <oasm/state.h> +#include <oasm/parse.h> +#define OASM_DBG +#include <oasm/log.h> + +struct oasm_state g_state; + +static void +oasm_start(struct oasm_state *state) +{ + state->line = 1; + parse_enter(state); +} + +int +main(int argc, char **argv) +{ + if (argc < 2) { + printf("oasm: usage: oasm <file> <output>\n"); + return -1; + } + + g_state.in_fd = open(argv[1], O_RDONLY); + if (g_state.in_fd < 0) { + printf("could not open \"%s\"\n", argv[1]); + return -1; + } + + g_state.filename = argv[1]; + oasm_start(&g_state); + close(g_state.in_fd); + return 0; +} diff --git a/usr.bin/oasm/parse.c b/usr.bin/oasm/parse.c new file mode 100644 index 0000000..6e7d2e6 --- /dev/null +++ b/usr.bin/oasm/parse.c @@ -0,0 +1,204 @@ +/* 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 <stdint.h> +#include <stddef.h> +#include <stdlib.h> +#include <oasm/state.h> +#include <oasm/lex.h> +#include <oasm/parse.h> +#include <oasm/log.h> + +static const char *tokstr[] = { + [ TT_UNKNOWN] = "bad", + [ TT_ADD ] = "add", + [ TT_SUB ] = "sub", + [ TT_MUL ] = "mul", + [ TT_DIV ] = "div", + [ TT_COMMA ] = ",", + [ TT_INC ] = "inc", + [ TT_DEC ] = "dec", + [ TT_MOV ] = "mov", + [ TT_IMM ] = "<imm>", + + /* X<n> registers */ + [ TT_X0 ] = "x0", + [ TT_X1 ] = "x1", + [ TT_X2 ] = "x2", + [ TT_X3 ] = "x3", + [ TT_X4 ] = "x4", + [ TT_X5 ] = "x5", + [ TT_X6 ] = "x6", + [ TT_X7 ] = "x7", + [ TT_X8 ] = "x8", + [ TT_X9 ] = "x9", + [ TT_X10 ] = "x10", + [ TT_X11 ] = "x11", + [ TT_X12 ] = "x12", + [ TT_X13 ] = "x13", + [ TT_X14 ] = "x14", + [ TT_X15 ] = "x15", + + /* V<n> registers */ + [ TT_F0 ] = "v0", + [ TT_F1 ] = "v1", + [ TT_F2 ] = "v2", + [ TT_F3 ] = "v3", + [ TT_F4 ] = "v4", + [ TT_F5 ] = "v5", + [ TT_F6 ] = "v6", + [ TT_F7 ] = "v7", + + /* D<n> registers */ + [ TT_D0 ] = "d0", + [ TT_D1 ] = "d1", + [ TT_D2 ] = "d2", + [ TT_D3 ] = "d3", + [ TT_D4 ] = "d4", + [ TT_D5 ] = "d5", + [ TT_D6 ] = "d6", + [ TT_D7 ] = "d7", + + /* V<n> registers */ + [ TT_V0 ] = "v0", + [ TT_V1 ] = "v1", + [ TT_V2 ] = "v2", + [ TT_V3 ] = "v3", + [ TT_V4 ] = "v4", + [ TT_V5 ] = "v5", + [ TT_V6 ] = "v6", + [ TT_V7 ] = "v7", +}; + +static int +parse_reg(struct oasm_state *state, struct oasm_token *tok) +{ + const char *p; + + /* Valid instructions that go with regs */ + switch (state->last) { + case TT_MOV: + case TT_DEC: + case TT_INC: + state->last = tok->type; + break; + default: + p = tokstr[state->last]; + oasm_err("bad instruction '%s' for regop\n", p); + return -1; + } + + switch (tok->type) { + case TT_X0: + case TT_X1: + case TT_X2: + case TT_X3: + case TT_X4: + case TT_X5: + case TT_X6: + case TT_X7: + case TT_X8: + case TT_X9: + case TT_X10: + case TT_X11: + case TT_X12: + case TT_X13: + case TT_X14: + state->last = tok->type; + break; + default: + p = tokstr[tok->type]; + oasm_err("bad register %s\n", p); + return -1; + } + + return 0; +} + +static int +parse_tok(struct oasm_state *state, struct oasm_token *tok) +{ + const char *p; + int error; + + switch (tok->type) { + case TT_MOV: + state->last = tok->type; + break; + case TT_DEC: + state->last = tok->type; + break; + case TT_IMM: + p = tokstr[TT_MOV]; + if (state->last != TT_MOV) { + oasm_err("previous token must be %s\n", p); + return -1; + } + break; + default: + if (!tok->is_reg) { + oasm_err("syntax error\n"); + return -1; + } + + error = parse_reg(state, tok); + if (error < 0) { + return error; + } + break; + } + + return 0; +} + +void +parse_enter(struct oasm_state *state) +{ + struct oasm_token tok; + const char *type, *raw; + int error = 0; + + for (;;) { + error = lex_tok(state, &tok); + if (error < 0) { + break; + } + + if (parse_tok(state, &tok) < 0) { + break; + } + + type = tokstr[tok.type]; + raw = tok.raw; + oasm_debug("got token type %s (%s)\n", type, raw); + + if (tok.raw != NULL) { + free(tok.raw); + } + } +} diff --git a/usr.bin/osh/osh.c b/usr.bin/osh/osh.c index 0f2108c..af7f4ab 100644 --- a/usr.bin/osh/osh.c +++ b/usr.bin/osh/osh.c @@ -30,6 +30,7 @@ #include <sys/types.h> #include <sys/cdefs.h> #include <sys/reboot.h> +#include <sys/errno.h> #include <sys/spawn.h> #include <sys/wait.h> #include <fcntl.h> @@ -66,7 +67,6 @@ #define PROMPT "[%s::osmora]~ " static char buf[64]; -static uint8_t buf_i; static int running; static int bell_fd; static bool bs_bell = true; /* Beep on backspace */ @@ -84,6 +84,15 @@ struct builtin_cmd { void (*func)(int argc, char *argv[]); }; +/* + * Results after parsing a command + * + * @bg: Run command in background + */ +struct parse_state { + uint8_t bg : 1; +}; + static struct builtin_cmd cmds[] = { {"help",cmd_help}, {"exit",cmd_exit}, @@ -146,7 +155,7 @@ cmd_bell(int argc, char *argv[]) } static int -parse_args(char *input, char *argv[], int max_args) +parse_args(char *input, char *argv[], int max_args, struct parse_state *p) { int argc = 0; @@ -155,6 +164,10 @@ parse_args(char *input, char *argv[], int max_args) return 0; } + /* setup default state */ + p->bg = 0; + + /* parse loop */ while (*input != '\0') { /* skip leading spaces */ while (*input == ' ') { @@ -171,6 +184,11 @@ parse_args(char *input, char *argv[], int max_args) break; } + /* run in background? */ + if (*input == '&') { + p->bg = 1; + } + if (argc < max_args) { argv[argc++] = input; /* mark start of the argument */ } @@ -194,14 +212,18 @@ parse_args(char *input, char *argv[], int max_args) return argc; } -static char * +/* + * Grab a string from stdin and return + * the resulting offset within the input + * buffer we are at. + */ +static uint8_t getstr(void) { char c; int input; uint32_t beep_payload; - - buf_i = 0; + uint8_t buf_i = 0; /* * Prepare the beep payload @ 500 Hz @@ -224,7 +246,7 @@ getstr(void) if (c == '\n') { buf[buf_i] = '\0'; putchar('\n'); - return buf; + return buf_i; } /* handle backspaces and DEL */ @@ -329,13 +351,46 @@ script_skip_comment(int fd) } } +/* + * Parse a single line typed in from the + * user. + * + * @input: Input line + */ +static int +parse_line(char *input) +{ + int argc; + char *argv[16]; + struct parse_state state = {0}; + pid_t child; + + /* Ensure the aux vector is zeored */ + memset(argv, 0, sizeof(argv)); + + /* + * Grab args from the user, there should be + * at least one. + */ + argc = parse_args(input, argv, sizeof(argv), &state); + if (argc == 0) { + return -EAGAIN; + } + + child = command_match(input, argc, argv); + if (child > 0 && !state.bg) { + waitpid(child, NULL, 0); + } + + return 0; +} + static int open_script(const char *pathname) { int fd, argc, buf_i = 0; - char c, *input, *argv[16]; + char c, *input; char buf[256]; - pid_t child; fd = open(pathname, O_RDONLY); if (fd < 0) { @@ -361,15 +416,7 @@ open_script(const char *pathname) if (c == '\n') { buf[buf_i] = '\0'; - argc = parse_args(buf, argv, sizeof(argv)); - - child = command_match(buf, argc, argv); - if (child > 0) { - waitpid(child, NULL, 0); - } - - argv[0] = NULL; - argv[1] = NULL; + parse_line(buf); buf_i = 0; continue; } @@ -384,7 +431,8 @@ main(int argc, char **argv) { int found, prog_argc; int stdout_fd; - char *input, *prog_argv[16], *p; + uint8_t buf_i; + char *p; char c; pid_t child; @@ -392,32 +440,23 @@ main(int argc, char **argv) return open_script(argv[1]); } - buf_i = 0; running = 1; - found = 0; bell_fd = open("/dev/beep", O_WRONLY); - puts(WELCOME); + while (running) { - memset(prog_argv, 0, sizeof(prog_argv)); printf(PROMPT, getlogin()); - input = getstr(); - if (input[0] == '\0') { + buf_i = getstr(); + if (buf[0] == '\0') { continue; } - prog_argc = parse_args(input, prog_argv, sizeof(prog_argv)); - if (prog_argc == 0) { + buf[buf_i] = '\0'; + if (parse_line(buf) < 0) { continue; } - child = command_match(input, prog_argc, prog_argv); - if (child > 0) { - waitpid(child, NULL, 0); - } - - found = 0; buf[0] = '\0'; } return 0; |