9os

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 81f60bd534bf81633396ed65a321a061aecac8e1
parent 075ceccf9dfb3115abe0ef4ae6e61012651ada0b
Author: Dimitris Papastamos <dimitris.papastamos@arm.com>
Date:   Wed, 21 Nov 2018 13:59:55 +0000

Merge changes from topic 'rv/dlang'

* changes:
  [libk] Rename kgetln() to kgets()
  [rmc] Move dbg() call after checking panic
  [rules.mk] Move FORALL with the other variables
  Move ec codes to private header
  [libk] Add libk.h header

Diffstat:
Ainclude/features.h | 18++++++++++++++++++
Ainclude/libk.h | 12++++++++++++
Minclude/rcode.h | 71++---------------------------------------------------------------------
Dscripts/genecstr.sh | 20--------------------
Mscripts/rules.mk | 19+++++++++----------
Msrc/Makefile | 4++--
Asrc/ec.h | 45+++++++++++++++++++++++++++++++++++++++++++++
Msrc/libk/Makefile | 2+-
Msrc/libk/__assert.c | 2+-
Msrc/libk/doprnt.c | 12++++--------
Msrc/libk/kgetc.c | 2+-
Dsrc/libk/kgetln.c | 20--------------------
Asrc/libk/kgets.c | 20++++++++++++++++++++
Msrc/libk/kprint.c | 4+++-
Msrc/libk/kputc.c | 2+-
Msrc/libk/kputs.c | 2+-
Msrc/libk/ksnprint.c | 4+++-
Msrc/libk/kvprint.c | 4+++-
Rsrc/libk/libk.h -> src/libk/stream.h | 0
Asrc/mkecstr | 20++++++++++++++++++++
Msrc/rmc.c | 5++++-
Msrc/romfw/dirfs.c | 1+
Msrc/romfw/dlang.c | 4+++-
23 files changed, 154 insertions(+), 139 deletions(-)

diff --git a/include/features.h b/include/features.h @@ -0,0 +1,18 @@ +#ifndef FEATURES_H_ +#define FEATURES_H_ + +#if __GNUC__ || __clang__ + #define KPRINTFMT __attribute__ ((format (printf, 1, 2))) + #define KSNPRINTFMT __attribute__ ((format (printf, 3, 4))) + #define noreturn __attribute__((__noreturn__)) +#else + #if __STDC_VERSION__ >= 201112L + #define noreturn _Noreturn + #else + #define noreturn + #endif + #define KPRINTFMT + #define KSNPRINTFMT +#endif + +#endif diff --git a/include/libk.h b/include/libk.h @@ -0,0 +1,12 @@ +#include <stdarg.h> +#include <stddef.h> + +#include "features.h" + +extern int kprint(const char *fmt, ...) KPRINTFMT; +extern int ksnprint(char *str, size_t len, const char *fmt, ...) KSNPRINTFMT; +extern char *kgets(char *s, int n); +extern int kgetc(void); +extern int kputc(int ch); +extern int kputs(const char *s); +extern int kvprint(const char *fmt, va_list va); diff --git a/include/rcode.h b/include/rcode.h @@ -3,19 +3,7 @@ #include <stddef.h> #include <stdint.h> -#if __GNUC__ || __clang__ - #define KPRINTFMT __attribute__ ((format (printf, 1, 2))) - #define KSNPRINTFMT __attribute__ ((format (printf, 3, 4))) - #define noreturn __attribute__((__noreturn__)) -#else - #if __STDC_VERSION__ >= 201112L - #define noreturn _Noreturn - #else - #define noreturn - #endif - #define KPRINTFMT - #define KSNPRINTFMT -#endif +#include "features.h" #ifndef NDEBUG #define dbg kprint @@ -37,52 +25,6 @@ typedef struct entry Entry; /* TODO: This must go away of here */ typedef struct file File; /* TODO: This must go away of here */ typedef struct fs Filsys; /* TODO: This must go away of here */ -enum ecvals { - UNKNOWN = 0x00, /* unknown reason */ - TRAPWFI = 0x01, /* trapped wfi or wfe */ - TRAPMCR = 0x03, /* trapped mcr or mrc (coproc == 1111) */ - TRAPMCRR = 0x04, /* trapped mcrr or mrrc (coproc == 1111) */ - TRAPMCR2 = 0x05, /* trapped mcr or mrc (coproc == 1110) */ - TRAPLDC = 0x06, /* trapped LDC or STC */ - TRAPSVE = 0x07, /* trapped SVE */ - TRAPVMRS = 0x08, /* trapped VMRS */ - TRAPPTRA = 0x09, /* trapped pointer autentication */ - TRAPMCRR2 = 0x0c, /* trapped mcrr or mrrc (coproc == 1110) */ - ILL = 0x0f, /* Illegal execution state */ - SVC32 = 0x11, /* SVC in AArch32 */ - HVC32 = 0x12, /* HVC in AArch32 */ - SMC32 = 0x13, /* SVC in AArch32 */ - SVC = 0x15, /* SVC */ - HVC = 0x16, /* HVC */ - SMC = 0x17, /* SMC */ - TRAPSYS = 0x18, /* Trapped MSR, MRS or System instruction */ - SVE = 0x19, /* SVE */ - TRAP = 0x1a, /* trapped ERET, ERETAA, ERETAB */ - EL3EXCP = 0x1f, /* implementation defined exception to EL3 */ - IABORTL = 0x20, /* Instruction abort from lower level */ - IABORT = 0x21, /* Instruction abort from current level */ - PCALIGN = 0x22, /* PC aligment fault exception */ - DABORTL = 0x24, /* Data abort from lower level */ - DABORT = 0x25, /* Data abort from current level */ - SPALIGN = 0x26, /* SP aligment fault exception */ - FPE32 = 0x28, /* Floating-point exception in AArch32 */ - FPE = 0x2c, /* Floating-point exception in AArch64 */ - SOFTRES = 0x2d, /* Software defined fault syndrome */ - RMC = 0x2e, /* RMC */ - SERROR = 0x2f, /* Serror interrupt */ - BREAKL = 0x30, /* Breakpoint exception from lower level */ - BREAK = 0x31, /* Breakpoint exception from current level */ - STEPL = 0x32, /* Software step from lower level */ - STEP = 0x33, /* Software step from current level */ - WATCHL = 0x34, /* Watchpoint from lower level */ - WATCH = 0x35, /* Watchpoint from current level */ - BKPT = 0x38, /* BKPT instruction in AArch32 */ - VECTOR = 0x3a, /* Vector catch exception in AArch32 */ - TRAPL = 0x3b, /* Trap from lower level */ - BRK = 0x3c, /* BRK instruction in AArch64 */ - NR_EC_VALS -}; - enum fsflags { FSCREATE = 1 << 0, FSREAD = 1 << 1, @@ -190,21 +132,12 @@ extern int seek(int fd, off_t off, int whence); extern int chdir(const char *path); extern int bind(const char *path, char *where); -/* libk */ -extern struct bssmap *bss(void); -extern int kprint(const char *fmt, ...) KPRINTFMT; -extern int ksnprint(char *str, size_t len, const char *fmt, ...) KSNPRINTFMT; -extern char *kgetln(char *s, int n); -extern int kgetc(void); -extern int kputc(int ch); -extern int kputs(const char *s); -extern int kvprint(const char *fmt, va_list va); - /* architectural functions */ extern noreturn void dohalt(void); extern noreturn void dopanic(void); extern noreturn void doswtch(struct trapframe *fp); extern void enaint(void); +extern struct bssmap *bss(void); extern uint8_t inm8(void *addr); extern uint16_t inm16(void *addr); extern uint32_t inm32(void *addr); diff --git a/scripts/genecstr.sh b/scripts/genecstr.sh @@ -1,20 +0,0 @@ -#!/bin/sh - -trap 'r=$?;rm -f $$.c;exit $r' EXIT HUP QUIT INT TERM - -awk ' -BEGIN {print "#include <rcode.h>\n"} -/^enum ecvals \{/ {print "const char *const ecstr[] = {" - print "#ifndef NDEBUG" - inhome = 1} - -inhome && /=/ {sub(/,/, "", $1) - printf("\t[%s] = \"", $1) - for (i = 5; i <= NF-1; ++i) - printf("%s%s", $i, (i == NF-1) ? "\"" : " ") - print ","} - -inhome && /^}/ {print "#endif" - print "\tNULL" - print "};" - inhome = 0}' $@ > $$.c && mv $$.c ecstr.c diff --git a/scripts/rules.mk b/scripts/rules.mk @@ -43,6 +43,15 @@ OC = $(CROSS_COMPILE)$(OBJCOPY) OD = $(CROSS_COMPILE)$(OBJDUMP) HOSTCC=$(COMP) +FORALL= +@set -e ;\ + pwd=$$PWD; \ + for i in $(DIRS); \ + do \ + cd $$i; \ + $(MAKE) $@; \ + cd $$pwd; \ + done + .SUFFIXES: .map .dump .elf .bin .i .c.o: @@ -69,16 +78,6 @@ HOSTCC=$(COMP) .elf.map: $(LD) -Map=$@ $< -o /dev/null - -FORALL= +@set -e ;\ - pwd=$$PWD; \ - for i in $(DIRS); \ - do \ - cd $$i; \ - $(MAKE) $@; \ - cd $$pwd; \ - done - all: FORCE: diff --git a/src/Makefile b/src/Makefile @@ -12,8 +12,8 @@ all: $(IMGS) gen: FORCE +@cd libtypes && $(MAKE) gen -ecstr.c: $(INCDIR)/rcode.h - $(SCRIPTDIR)/genecstr.sh $(INCDIR)/rcode.h +ecstr.c: ec.h + ./mkecstr ec.h $(IMGS): $(LIBS) $(OBJS) diff --git a/src/ec.h b/src/ec.h @@ -0,0 +1,45 @@ +enum ecvals { + UNKNOWN = 0x00, /* unknown reason */ + TRAPWFI = 0x01, /* trapped wfi or wfe */ + TRAPMCR = 0x03, /* trapped mcr or mrc (coproc == 1111) */ + TRAPMCRR = 0x04, /* trapped mcrr or mrrc (coproc == 1111) */ + TRAPMCR2 = 0x05, /* trapped mcr or mrc (coproc == 1110) */ + TRAPLDC = 0x06, /* trapped LDC or STC */ + TRAPSVE = 0x07, /* trapped SVE */ + TRAPVMRS = 0x08, /* trapped VMRS */ + TRAPPTRA = 0x09, /* trapped pointer autentication */ + TRAPMCRR2 = 0x0c, /* trapped mcrr or mrrc (coproc == 1110) */ + ILL = 0x0f, /* Illegal execution state */ + SVC32 = 0x11, /* SVC in AArch32 */ + HVC32 = 0x12, /* HVC in AArch32 */ + SMC32 = 0x13, /* SVC in AArch32 */ + SVC = 0x15, /* SVC */ + HVC = 0x16, /* HVC */ + SMC = 0x17, /* SMC */ + TRAPSYS = 0x18, /* Trapped MSR, MRS or System instruction */ + SVE = 0x19, /* SVE */ + TRAP = 0x1a, /* trapped ERET, ERETAA, ERETAB */ + EL3EXCP = 0x1f, /* implementation defined exception to EL3 */ + IABORTL = 0x20, /* Instruction abort from lower level */ + IABORT = 0x21, /* Instruction abort from current level */ + PCALIGN = 0x22, /* PC aligment fault exception */ + DABORTL = 0x24, /* Data abort from lower level */ + DABORT = 0x25, /* Data abort from current level */ + SPALIGN = 0x26, /* SP aligment fault exception */ + FPE32 = 0x28, /* Floating-point exception in AArch32 */ + FPE = 0x2c, /* Floating-point exception in AArch64 */ + SOFTRES = 0x2d, /* Software defined fault syndrome */ + RMC = 0x2e, /* RMC */ + SERROR = 0x2f, /* Serror interrupt */ + BREAKL = 0x30, /* Breakpoint exception from lower level */ + BREAK = 0x31, /* Breakpoint exception from current level */ + STEPL = 0x32, /* Software step from lower level */ + STEP = 0x33, /* Software step from current level */ + WATCHL = 0x34, /* Watchpoint from lower level */ + WATCH = 0x35, /* Watchpoint from current level */ + BKPT = 0x38, /* BKPT instruction in AArch32 */ + VECTOR = 0x3a, /* Vector catch exception in AArch32 */ + TRAPL = 0x3b, /* Trap from lower level */ + BRK = 0x3c, /* BRK instruction in AArch64 */ + NR_EC_VALS +}; diff --git a/src/libk/Makefile b/src/libk/Makefile @@ -9,7 +9,7 @@ OBJS = doprnt.o \ kputc.o \ kputs.o \ kvprint.o \ - kgetln.o \ + kgets.o \ __assert.o \ TARGET = $(LIBDIR)/libk.a diff --git a/src/libk/__assert.c b/src/libk/__assert.c @@ -1,7 +1,7 @@ #include <assert.h> #include <stdlib.h> -extern void kprint(const char *fmt, ...); +#include <libk.h> void __assert(char *exp, char *file, long line) { diff --git a/src/libk/doprnt.c b/src/libk/doprnt.c @@ -1,22 +1,19 @@ #include <stdarg.h> #include <stddef.h> -#include <rcode.h> -#include <rcode.h> +#include <libk.h> -#include "libk.h" +#include "stream.h" #include "../libc/syscall.h" static void putch(Stream *sp, int c) { - char ch; - if (sp->fd < 0) { if (sp->cnt < sp->len) sp->base[sp->cnt++] = c; } else { - kputc(ch); + kputc(c); sp->cnt++; } } @@ -36,10 +33,9 @@ printn(Stream *sp, long long n, int base, int sign) div = 10000000000000000000u; break; case 16: + default: div = 0x1000000000000000u; break; - default: - panic("invalid base"); } if (sign && n < 0) { diff --git a/src/libk/kgetc.c b/src/libk/kgetc.c @@ -1,6 +1,6 @@ #include <stdio.h> -#include <rcode.h> +#include <libk.h> #include "../libc/syscall.h" diff --git a/src/libk/kgetln.c b/src/libk/kgetln.c @@ -1,20 +0,0 @@ -#include <stdio.h> - -#include <rcode.h> - -char * -kgetln(char *s, int n) -{ - int ch = 0; - char *t = s; - - while (--n > 0 && (ch = kgetc()) != EOF) { - if ((*t++ = ch) == '\n') - break; - } - if (ch == EOF && s == t) - return NULL; - *t = '\0'; - - return s; -} diff --git a/src/libk/kgets.c b/src/libk/kgets.c @@ -0,0 +1,20 @@ +#include <stdio.h> + +#include <libk.h> + +char * +kgets(char *s, int n) +{ + int ch = 0; + char *t = s; + + while (--n > 0 && (ch = kgetc()) != EOF) { + if ((*t++ = ch) == '\n') + break; + } + if (ch == EOF && s == t) + return NULL; + *t = '\0'; + + return s; +} diff --git a/src/libk/kprint.c b/src/libk/kprint.c @@ -1,6 +1,8 @@ #include <stdarg.h> -#include "libk.h" +#include <libk.h> + +#include "stream.h" int kprint(const char *fmt, ...) diff --git a/src/libk/kputc.c b/src/libk/kputc.c @@ -1,6 +1,6 @@ #include <stdio.h> -#include <rcode.h> +#include <libk.h> #include "../libc/syscall.h" diff --git a/src/libk/kputs.c b/src/libk/kputs.c @@ -1,6 +1,6 @@ #include <stdio.h> -#include <rcode.h> +#include <libk.h> int kputs(const char *s) diff --git a/src/libk/ksnprint.c b/src/libk/ksnprint.c @@ -1,7 +1,9 @@ #include <stdarg.h> #include <stddef.h> -#include "libk.h" +#include <libk.h> + +#include "stream.h" int ksnprint(char *str, size_t len, const char *fmt, ...) diff --git a/src/libk/kvprint.c b/src/libk/kvprint.c @@ -1,7 +1,9 @@ #include <stdarg.h> #include <stddef.h> -#include "libk.h" +#include <libk.h> + +#include "stream.h" int kvprint(const char *fmt, va_list va) diff --git a/src/libk/libk.h b/src/libk/stream.h diff --git a/src/mkecstr b/src/mkecstr @@ -0,0 +1,20 @@ +#!/bin/sh + +trap 'r=$?;rm -f $$.c;exit $r' EXIT HUP QUIT INT TERM + +awk ' +BEGIN {print "#include \"ec.h\"\n"} +/^enum ecvals \{/ {print "const char *const ecstr[] = {" + print "#ifndef NDEBUG" + inhome = 1} + +inhome && /=/ {sub(/,/, "", $1) + printf("\t[%s] = \"", $1) + for (i = 5; i <= NF-1; ++i) + printf("%s%s", $i, (i == NF-1) ? "\"" : " ") + print ","} + +inhome && /^}/ {print "#endif" + print "\t0" + print "};" + inhome = 0}' $@ > $$.c && mv $$.c ecstr.c diff --git a/src/rmc.c b/src/rmc.c @@ -1,6 +1,9 @@ +#include <libk.h> #include <rmu.h> #include <rcode.h> +#include "ec.h" + static void dumpregs(struct trapframe *fp) { @@ -99,11 +102,11 @@ trap(struct trapframe *fp) enum ecvals ec; const char *msg; - dbg("exception handler\n"); if (bss->errstr) { panicfmt(bss->errstr, fp); halt(); } + dbg("exception handler\n"); ec = (fp->esr >> 26) & 0x3f; diff --git a/src/romfw/dirfs.c b/src/romfw/dirfs.c @@ -1,6 +1,7 @@ #include <errno.h> #include <string.h> +#include <libk.h> #include <rcode.h> #include "fs.h" diff --git a/src/romfw/dlang.c b/src/romfw/dlang.c @@ -6,8 +6,10 @@ #include <stdlib.h> #include <string.h> +#include <libk.h> #include <rcode.h> +#include "../ec.h" #include "dlang.h" #define PREFIX "> " @@ -309,7 +311,7 @@ run(struct args *args) size_t len; char buffer[BUFSIZ]; - if (kgetln(buffer, sizeof(buffer)) == NULL) { + if (kgets(buffer, sizeof(buffer)) == NULL) { args->status = 1; return -1; }