Skip to content

Commit

Permalink
Improves altio
Browse files Browse the repository at this point in the history
  • Loading branch information
HAKarlsson committed Nov 9, 2023
1 parent 23d7925 commit 45ec901
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 151 deletions.
5 changes: 4 additions & 1 deletion common/inc/altc/altio.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <stdarg.h>
#include <stddef.h>

int alt_getchar(void);
Expand All @@ -7,4 +8,6 @@ int alt_putstr(const char *str);
int alt_puts(const char *str);
int alt_gets(char *str);
int alt_printf(const char *fmt, ...);
int alt_snprintf(char *restrict s, size_t n, char *restrict fmt, ...);
int alt_snprintf(char *restrict s, size_t n, const char *restrict fmt, ...);
int alt_vsnprintf(char *restrict s, size_t n, const char *restrict fmt,
va_list ap);
139 changes: 95 additions & 44 deletions common/src/altc/printf.c
Original file line number Diff line number Diff line change
@@ -1,67 +1,118 @@
#include "altc/altio.h"

#include <stdarg.h>
#include <stddef.h>

int alt_printf(const char *fmt, ...)
#define ALT_PRINTF_BUF_SIZE 128

static char *write_dec(char *restrict dst, const char *end,
unsigned long long val)
{
if (!val && dst != end) {
*(dst++) = '0';
} else if (dst != end) {
int i = 0;
char buf[32];
while (val) {
int tmp = val % 10;
buf[i++] = '0' + tmp;
val /= 10;
}
while (i > 0 && dst != end)
*(dst++) = buf[--i];
}
return dst;
}

static char *write_hex(char *restrict dst, const char *end,
unsigned long long val)
{
if (!val && dst != end) {
*(dst++) = '0';
} else if (dst != end) {
int i = 0;
char buf[16];
while (val) {
int tmp = val & 0xF;
buf[i++] = tmp < 10 ? ('0' + tmp) : 'A' + (tmp - 10);
val >>= 4;
}
while (i > 0 && dst != end)
*(dst++) = buf[--i];
}
return dst;
}

static char *write_str(char *restrict dst, const char *end, char *restrict src)
{
static const char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
unsigned long long x;
int len;
va_list args;
va_start(args, fmt);
len = 0;
while (dst != end && *src != '\0')
*(dst++) = *(src++);
return dst;
}

while (*fmt != '\0') {
if (*fmt++ != '%') {
alt_putchar(*(fmt - 1));
len++;
static char *write_char(char *restrict dst, const char *end, char c)
{
if (dst != end)
*(dst++) = c;
return dst;
}

int alt_vsnprintf(char *restrict str, size_t size, const char *restrict fmt,
va_list ap)
{
char *s = str;
const char *end = str + size - 1;
while (*fmt != '\0' && s != end) {
if (*(fmt++) != '%') {
s = write_char(s, end, *(fmt - 1));
continue;
}
switch (*fmt++) {
switch (*(fmt++)) {
case '%':
s = write_char(s, end, '%');
break;
case 'c':
alt_putchar((char)va_arg(args, int));
len++;
s = write_char(s, end, va_arg(ap, int));
break;
case 's':
len += alt_putstr(va_arg(args, char *));
s = write_str(s, end, va_arg(ap, char *));
break;
case 'x':
x = va_arg(args, unsigned int);
if (!x) {
alt_putchar('0');
len++;
break;
}
for (int i = 28; i >= 0; i -= 4) {
if (x >> i) {
alt_putchar(hex[(x >> i) & 0xF]);
len++;
}
}
s = write_hex(s, end, va_arg(ap, unsigned int));
break;
case 'X':
x = va_arg(args, unsigned long long);
if (!x) {
alt_putchar('0');
len++;
break;
}
for (int i = 60; i >= 0; i -= 4) {
if (x >> i) {
alt_putchar(hex[(x >> i) & 0xF]);
len++;
}
}
s = write_hex(s, end, va_arg(ap, unsigned long long));
break;
case '%':
alt_putchar('%');
len++;
case 'd':
s = write_dec(s, end, va_arg(ap, unsigned int));
break;
case 'D':
s = write_dec(s, end, va_arg(ap, unsigned long long));
break;
case '\0':
break;
}
}
va_end(args);
*s = '\0';
return s - str;
}

int alt_snprintf(char *restrict str, size_t size, const char *restrict fmt, ...)
{
va_list ap;
va_start(ap, fmt);
int len = alt_vsnprintf(str, size, fmt, ap);
va_end(ap);
return len;
}

int alt_printf(const char *restrict fmt, ...)
{
char buf[ALT_PRINTF_BUF_SIZE + 1];
va_list ap;
va_start(ap, fmt);
int len = alt_vsnprintf(buf, ALT_PRINTF_BUF_SIZE + 1, fmt, ap);
va_end(ap);
alt_puts(buf);
return len;
}
71 changes: 0 additions & 71 deletions common/src/altc/snprintf.c

This file was deleted.

4 changes: 3 additions & 1 deletion kernel/inc/cap_util.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
#include "cap_types.h"

#include <stddef.h>

cap_t cap_mk_time(hart_t hart, time_slot_t bgn, time_slot_t end);
cap_t cap_mk_memory(addr_t bgn, addr_t end, rwx_t rwx);
cap_t cap_mk_pmp(napot_t addr, rwx_t rwx);
Expand Down Expand Up @@ -29,4 +31,4 @@ static inline uint64_t pmp_napot_encode(uint64_t base, uint64_t size)

bool cap_is_valid(cap_t cap);
bool cap_is_derivable(cap_t parent, cap_t child);
void cap_print(cap_t cap);
void cap_snprint(char *restrict buf, size_t size, cap_t cap);
5 changes: 2 additions & 3 deletions kernel/inc/kassert.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "altc/altio.h"
#include "kprintf.h"

/**
* @file kassert.h
Expand All @@ -17,9 +17,8 @@
*/
#ifndef NDEBUG

#define _X_(x) #x
#define KASSERT_FAILURE(FILE, LINE) \
alt_puts("Kernel assertion failed at " FILE ":" _X_(LINE) ".");
kprintf("Kernel assertion failed at %s:%d.\n", FILE, LINE);

#define KASSERT(EXPR) \
do { \
Expand Down
3 changes: 3 additions & 0 deletions kernel/inc/kprintf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once
void kputstr(char *s);
void kprintf(char *restrict fmt, ...);
8 changes: 4 additions & 4 deletions kernel/src/cap_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ void ctable_init(void)
const cap_t init_caps[] = INIT_CAPS;
cte_t prev = ctable;
#if !defined(NDEBUG) && VERBOSE > 0
alt_puts("> Initial capabilities:");
kputstr("> Initial capabilities:\n");
#endif
for (unsigned int i = 0; i < ARRAY_SIZE(init_caps); ++i) {
#if !defined(NDEBUG) && VERBOSE > 0
alt_printf(" 0x%x: ", i);
cap_print(init_caps[i]);
alt_printf("\n");
char buf[128];
cap_snprint(buf, 128, init_caps[i]);
kprintf(" %d: %s\n", i, buf);
#endif
cte_insert(&ctable[i], init_caps[i], prev);
}
Expand Down
42 changes: 23 additions & 19 deletions kernel/src/cap_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,45 +82,49 @@ cap_t cap_mk_socket(chan_t chan, ipc_mode_t mode, ipc_perm_t perm, uint32_t tag)
return cap;
}

void cap_print(cap_t cap)
void cap_snprint(char *restrict buf, size_t size, cap_t cap)
{
switch (cap.type) {
case CAPTY_NONE:
alt_printf("NONE{}");
alt_snprintf(buf, size, "NONE{}");
break;
case CAPTY_TIME:
alt_printf("TIME{hart=%x,bgn=%x,end=%x,mrk=%x}", cap.time.hart,
cap.time.bgn, cap.time.end, cap.time.mrk);
alt_snprintf(buf, size, "TIME{hart=%d,bgn=%d,end=%d,mrk=%d}",
cap.time.hart, cap.time.bgn, cap.time.end,
cap.time.mrk);
break;
case CAPTY_MEMORY: {
uint64_t bgn = tag_block_to_addr(cap.mem.tag, cap.mem.bgn);
uint64_t end = tag_block_to_addr(cap.mem.tag, cap.mem.end);
uint64_t mrk = tag_block_to_addr(cap.mem.tag, cap.mem.mrk);
alt_printf("MEMORY{bgn=%X,end=%X,mrk=%x,rwx=%X,lck=%x}", bgn,
end, mrk, cap.mem.rwx, cap.mem.lck);
alt_snprintf(buf, size,
"MEMORY{bgn=0x%X,end=0x%X,mrk=0x%X,rwx=%d,lck=%x}",
bgn, end, mrk, cap.mem.rwx, cap.mem.lck);
} break;
case CAPTY_PMP: {
uint64_t base, size;
pmp_napot_decode(cap.pmp.addr, &base, &size);
alt_printf("PMP{bgn=%X,end=%X,rwx=%x,used=%x,slot=%x}", base,
base + size, cap.pmp.rwx, cap.pmp.used,
cap.pmp.slot);
uint64_t base, _size;
pmp_napot_decode(cap.pmp.addr, &base, &_size);
alt_snprintf(buf, size,
"PMP{bgn=0x%X,end=0x%X,rwx=%d,used=%d,slot=%d}",
base, base + _size, cap.pmp.rwx, cap.pmp.used,
cap.pmp.slot);
} break;
case CAPTY_MONITOR:
alt_printf("MONITOR{bgn=%x,end=%x,mrk=%x}", cap.mon.bgn,
cap.mon.end, cap.mon.mrk);
alt_snprintf(buf, size, "MONITOR{bgn=%d,end=%d,mrk=%d}",
cap.mon.bgn, cap.mon.end, cap.mon.mrk);
break;
case CAPTY_CHANNEL:
alt_printf("CHANNEL{bgn=%x,end=%x,mrk=%x}", cap.chan.bgn,
cap.chan.end, cap.chan.mrk);
alt_snprintf(buf, size, "CHANNEL{bgn=%d,end=%d,mrk=%d}",
cap.chan.bgn, cap.chan.end, cap.chan.mrk);
break;
case CAPTY_SOCKET:
alt_printf("SOCKET{chan=%x,tag=%x,perm=%x,mode=%x}",
cap.sock.chan, cap.sock.tag, cap.sock.perm,
cap.sock.mode);
alt_snprintf(buf, size,
"SOCKET{chan=%d,tag=%d,perm=%d,mode=%d}",
cap.sock.chan, cap.sock.tag, cap.sock.perm,
cap.sock.mode);
break;
default:
alt_printf("UNKNOWN{raw=%X}", cap.raw);
alt_snprintf(buf, size, "UNKNOWN{raw=0x%X}", cap.raw);
}
}

Expand Down
4 changes: 4 additions & 0 deletions kernel/src/exception.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

proc_t *handle_exception(proc_t *p)
{
#if !defined(NDEBUG) && VERBOSE > 1
kprintf("handle_exception(pid=%X,mcause=%X,mtval=%X,mepc=%X)", p->pid,
csrr_mcause(), csrr_mtval(), csrr_mepc());
#endif
/* Check if it is a return from exception */
p->regs[REG_ECAUSE] = csrr_mcause();
p->regs[REG_EVAL] = csrr_mtval();
Expand Down
Loading

0 comments on commit 45ec901

Please sign in to comment.