From c931384e30de2b79acbb5ec6fc3ad32e9c0313b5 Mon Sep 17 00:00:00 2001 From: bLueriVerLHR Date: Mon, 26 Jun 2023 09:56:22 +0800 Subject: [PATCH] [update] os --- laos/Makefile | 8 +++-- laos/include/dev.h | 6 ++++ laos/include/filepath.h | 9 +++++ laos/include/la32r.h | 22 ++++++------ laos/include/memlayout.h | 2 +- laos/kernel.ld | 1 + laos/src/dev/dev.c | 3 ++ laos/src/kernel/console.c | 26 +++++++------- laos/src/user/xargs.c | 8 ++--- laos/src/util/filepath.c | 71 +++++++++++++++++++++++++++++++++++++++ 10 files changed, 125 insertions(+), 31 deletions(-) create mode 100644 laos/include/dev.h create mode 100644 laos/include/filepath.h create mode 100644 laos/src/dev/dev.c create mode 100644 laos/src/util/filepath.c diff --git a/laos/Makefile b/laos/Makefile index a783c06..dcaea65 100644 --- a/laos/Makefile +++ b/laos/Makefile @@ -28,15 +28,16 @@ LD_CSRIPT := kernel.ld EXTRA_MACRO := # NDEBUG EXTRA_FLAGS = -Wall -Werror -O0 -fno-omit-frame-pointer -ggdb -gdwarf-2 +EXTRA_FLAGS += -mfpu=none -march=loongarch32r -mabi=ilp32s EXTRA_FLAGS += -MD EXTRA_FLAGS += -ffreestanding -fno-common -nostdlib EXTRA_FLAGS += -I. EXTRA_FLAGS += -fno-pie -no-pie EXTRA_FLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector) -COMMONFLAGS := $(EXTRA_FLAGS) -march=loongarch32r +COMMONFLAGS := $(EXTRA_FLAGS) CPPFLAGS += $(addprefix -D,$(EXTRA_MACRO)) -I$(CURDIR)/include -CFLAGS += $(COMMONFLAGS) -O +CFLAGS += $(COMMONFLAGS) LDFLAGS += $(COMMONFLAGS) -z max-page-size=4096 -T$(LD_CSRIPT) .PHONY: all build clean @@ -70,3 +71,6 @@ usr: @TOP_BUILD_DIR=$(ABS_BUILD) \ CPPFLAGS="$(CPPFLAGS)" \ $(MAKE) native -C $(TOP_SRC_DIR)/user -j$(NPROC) + +tcpl: + $(CC) --target-help diff --git a/laos/include/dev.h b/laos/include/dev.h new file mode 100644 index 0000000..98b0874 --- /dev/null +++ b/laos/include/dev.h @@ -0,0 +1,6 @@ +#ifndef DEV_H__ +#define DEV_H__ + +extern char ioports[]; + +#endif \ No newline at end of file diff --git a/laos/include/filepath.h b/laos/include/filepath.h new file mode 100644 index 0000000..7e989c5 --- /dev/null +++ b/laos/include/filepath.h @@ -0,0 +1,9 @@ +#ifndef FILEPATH_H__ +#define FILEPATH_H__ + + +char *basename(char *bnm, const char *path); +char *rmext(char *noext, const char *fname); +char *bearname(char *bnm, const char *fname); + +#endif \ No newline at end of file diff --git a/laos/include/la32r.h b/laos/include/la32r.h index 5c06a6d..563cd92 100644 --- a/laos/include/la32r.h +++ b/laos/include/la32r.h @@ -1,8 +1,16 @@ + + #include "asm.h" #include "defs.h" #include "latype.h" #include "macro.h" +#ifndef __loongarch32r +#define __loongarch32r +#endif + +#include "larchintrin.h" + #ifndef LA32R_H__ #define LA32R_H__ @@ -56,18 +64,10 @@ static inline uint r_ra() { // mem misc // -static inline void dbar() { - asm volatile("dbar 0"); -} - -static inline void ibar() { - asm volatile("ibar 0"); -} - // need to be tested static inline void synchronize() { - dbar(); - ibar(); + __dbar(0); + __ibar(0); } @@ -101,4 +101,6 @@ static inline uint scw(uint wdata, intptr_t addr) { return recv; } + + #endif \ No newline at end of file diff --git a/laos/include/memlayout.h b/laos/include/memlayout.h index 214a31d..88be45b 100644 --- a/laos/include/memlayout.h +++ b/laos/include/memlayout.h @@ -4,6 +4,6 @@ #define DEV_BASE_ADDR 0xa0000000 -#define SERIAL_ADDR (DEV_BASE_ADDR + 0x00000000) +#define SERIAL_OFFSET 0x00000000 #endif \ No newline at end of file diff --git a/laos/kernel.ld b/laos/kernel.ld index 2ecfb57..8212615 100644 --- a/laos/kernel.ld +++ b/laos/kernel.ld @@ -38,4 +38,5 @@ SECTIONS } PROVIDE(end = .); + PROVIDE(ioports = 0xa0000000); } diff --git a/laos/src/dev/dev.c b/laos/src/dev/dev.c new file mode 100644 index 0000000..481bc16 --- /dev/null +++ b/laos/src/dev/dev.c @@ -0,0 +1,3 @@ +#include "dev.h" + +char ioports[]; diff --git a/laos/src/kernel/console.c b/laos/src/kernel/console.c index 85aadc4..8c697ba 100644 --- a/laos/src/kernel/console.c +++ b/laos/src/kernel/console.c @@ -2,27 +2,27 @@ #include "memio.h" #include "memlayout.h" #include "spinlock.h" +#include "dev.h" void consputc(int c) { - if (c == '\b') { - memb(SERIAL_ADDR) = '\b'; - memb(SERIAL_ADDR) = ' '; - memb(SERIAL_ADDR) = '\b'; - return; - } + volatile char *out = &ioports[SERIAL_OFFSET]; + if (c == '\b') { + *out = '\b'; + *out = ' '; + *out = '\b'; + return; + } - memb(SERIAL_ADDR) = c; + *out = c; } struct { struct spinlock lock; - + // input #define INPUT_BUF_SIZE 128 char buf[INPUT_BUF_SIZE]; - uint r; // Read index - uint w; // Write index - uint e; // Edit index + uint r; // Read index + uint w; // Write index + uint e; // Edit index } cons; - - diff --git a/laos/src/user/xargs.c b/laos/src/user/xargs.c index dec1b45..172dbc6 100644 --- a/laos/src/user/xargs.c +++ b/laos/src/user/xargs.c @@ -29,11 +29,9 @@ int main(int argc, char *argv[]) { char *tok = strtok(input, " "); while (tok) { - if (*tok != '\n') { - args = realloc(args, (argc + 1) * sizeof(char *)); - args[argc] = tok; - argc += 1; - } + args = realloc(args, (argc + 1) * sizeof(char *)); + args[argc] = tok; + argc += 1; tok = strtok(NULL, " "); } diff --git a/laos/src/util/filepath.c b/laos/src/util/filepath.c new file mode 100644 index 0000000..c491943 --- /dev/null +++ b/laos/src/util/filepath.c @@ -0,0 +1,71 @@ +#include "defs.h" + +// +// bnm should be large enough to hold basename +// +char *basename(char *bnm, const char *path) { + const char *p = path; + const char *last_slash = NULL; + while (*p) { + if (*p == '/' || *p == '\\') { + last_slash = p; + } + p++; + } + + char *q = bnm; + p = last_slash + 1; + while (*p) { + *q++ = *p++; + } + + return bnm; +} + +// +// noext should be large enough to hold no-ext name +// +char *rmext(char *noext, const char *fname) { + const char *p = fname; + const char *last_dot = NULL; + while (*p) { + if (*p == '.') { + last_dot = p; + } + p++; + } + + char *q = noext; + p = fname; + while (p != last_dot) { + *q++ = *p++; + } + + return noext; +} + + +// +// bnm should be large enough to hold bearname name +// +char *bearname(char *bnm, const char *fname) { + const char *p = fname; + const char *last_slash = NULL; + const char *last_dot = NULL; + while (*p) { + if (*p == '/' || *p == '\\') { + last_slash = p; + } else if (*p == '.') { + last_dot = p; + } + p++; + } + + char *q = bnm; + p = last_slash + 1; + while (p != last_dot) { + *q++ = *p++; + } + + return bnm; +} \ No newline at end of file