Mass reformat

This commit is contained in:
Imbus 2025-09-01 22:43:50 +02:00
parent 3450ab2cb8
commit f1fd7de79f
10 changed files with 317 additions and 297 deletions

View file

@ -1,10 +1,10 @@
#include <spinlock.h>
#include <kalloc.h> #include <kalloc.h>
#include <memory.h> #include <memory.h>
#include <panic.h> #include <panic.h>
#include <riscv.h> #include <riscv.h>
#include <string.h> #include <spinlock.h>
#include <stdint.h> #include <stdint.h>
#include <string.h>
// Physical memory allocator, for user processes, // Physical memory allocator, for user processes,
// kernel stacks, page-table pages, // kernel stacks, page-table pages,

View file

@ -52,24 +52,12 @@ int kvsnprintf(char *buf, size_t size, const char *fmt, va_list args) {
fmt++; // skip '%' fmt++; // skip '%'
switch (*fmt) { switch (*fmt) {
case 's': case 's': append_str(&p, &remaining, va_arg(args, const char *)); break;
append_str(&p, &remaining, va_arg(args, const char *)); case 'd': append_int(&p, &remaining, va_arg(args, int), 10); break;
break; case 'x': append_int(&p, &remaining, va_arg(args, unsigned int), 16); break;
case 'd': case 'c': append_char(&p, &remaining, (char)va_arg(args, int)); break;
append_int(&p, &remaining, va_arg(args, int), 10); case '%': append_char(&p, &remaining, '%'); break;
break; default: append_char(&p, &remaining, '?'); break;
case 'x':
append_int(&p, &remaining, va_arg(args, unsigned int), 16);
break;
case 'c':
append_char(&p, &remaining, (char)va_arg(args, int));
break;
case '%':
append_char(&p, &remaining, '%');
break;
default:
append_char(&p, &remaining, '?');
break;
} }
fmt++; fmt++;
} }

View file

@ -4,88 +4,133 @@
#include <types.h> #include <types.h>
/** Swap byte order of 16-bit value */ /** Swap byte order of 16-bit value */
static inline u16 swap16(u16 x) { return (x >> 8) | (x << 8); } static inline u16 swap16(u16 x) {
return (x >> 8) | (x << 8);
}
/** Swap byte order of 32-bit value */ /** Swap byte order of 32-bit value */
static inline u32 swap32(u32 x) { static inline u32 swap32(u32 x) {
return ((x >> 24) & 0x000000ff) | ((x >> 8) & 0x0000ff00) | return ((x >> 24) & 0x000000ff) | ((x >> 8) & 0x0000ff00) | ((x << 8) & 0x00ff0000) | ((x << 24) & 0xff000000);
((x << 8) & 0x00ff0000) | ((x << 24) & 0xff000000);
} }
/** Swap byte order of 64-bit value */ /** Swap byte order of 64-bit value */
static inline u64 swap64(u64 x) { static inline u64 swap64(u64 x) {
return ((x >> 56) & 0x00000000000000ffULL) | return ((x >> 56) & 0x00000000000000ffULL) | ((x >> 40) & 0x000000000000ff00ULL) |
((x >> 40) & 0x000000000000ff00ULL) | ((x >> 24) & 0x0000000000ff0000ULL) | ((x >> 8) & 0x00000000ff000000ULL) |
((x >> 24) & 0x0000000000ff0000ULL) | ((x << 8) & 0x000000ff00000000ULL) | ((x << 24) & 0x0000ff0000000000ULL) |
((x >> 8) & 0x00000000ff000000ULL) | ((x << 40) & 0x00ff000000000000ULL) | ((x << 56) & 0xff00000000000000ULL);
((x << 8) & 0x000000ff00000000ULL) |
((x << 24) & 0x0000ff0000000000ULL) |
((x << 40) & 0x00ff000000000000ULL) |
((x << 56) & 0xff00000000000000ULL);
} }
#ifdef __LITTLE_ENDIAN__ #ifdef __LITTLE_ENDIAN__
/** Convert 16-bit value to little-endian */ /** Convert 16-bit value to little-endian */
static inline u16 to_le16(u16 x) { return x; } static inline u16 to_le16(u16 x) {
return x;
}
/** Convert 16-bit little-endian value to host */ /** Convert 16-bit little-endian value to host */
static inline u16 from_le16(u16 x) { return x; } static inline u16 from_le16(u16 x) {
return x;
}
/** Convert 32-bit value to little-endian */ /** Convert 32-bit value to little-endian */
static inline u32 to_le32(u32 x) { return x; } static inline u32 to_le32(u32 x) {
return x;
}
/** Convert 32-bit little-endian value to host */ /** Convert 32-bit little-endian value to host */
static inline u32 from_le32(u32 x) { return x; } static inline u32 from_le32(u32 x) {
return x;
}
/** Convert 64-bit value to little-endian */ /** Convert 64-bit value to little-endian */
static inline u64 to_le64(u64 x) { return x; } static inline u64 to_le64(u64 x) {
return x;
}
/** Convert 64-bit little-endian value to host */ /** Convert 64-bit little-endian value to host */
static inline u64 from_le64(u64 x) { return x; } static inline u64 from_le64(u64 x) {
return x;
}
/** Convert 16-bit value to big-endian */ /** Convert 16-bit value to big-endian */
static inline u16 to_be16(u16 x) { return swap16(x); } static inline u16 to_be16(u16 x) {
return swap16(x);
}
/** Convert 16-bit big-endian value to host */ /** Convert 16-bit big-endian value to host */
static inline u16 from_be16(u16 x) { return swap16(x); } static inline u16 from_be16(u16 x) {
return swap16(x);
}
/** Convert 32-bit value to big-endian */ /** Convert 32-bit value to big-endian */
static inline u32 to_be32(u32 x) { return swap32(x); } static inline u32 to_be32(u32 x) {
return swap32(x);
}
/** Convert 32-bit big-endian value to host */ /** Convert 32-bit big-endian value to host */
static inline u32 from_be32(u32 x) { return swap32(x); } static inline u32 from_be32(u32 x) {
return swap32(x);
}
/** Convert 64-bit value to big-endian */ /** Convert 64-bit value to big-endian */
static inline u64 to_be64(u64 x) { return swap64(x); } static inline u64 to_be64(u64 x) {
return swap64(x);
}
/** Convert 64-bit big-endian value to host */ /** Convert 64-bit big-endian value to host */
static inline u64 from_be64(u64 x) { return swap64(x); } static inline u64 from_be64(u64 x) {
return swap64(x);
}
#else // Big-endian #else // Big-endian
/** Convert 16-bit value to little-endian */ /** Convert 16-bit value to little-endian */
static inline u16 to_le16(u16 x) { return swap16(x); } static inline u16 to_le16(u16 x) {
return swap16(x);
}
/** Convert 16-bit little-endian value to host */ /** Convert 16-bit little-endian value to host */
static inline u16 from_le16(u16 x) { return swap16(x); } static inline u16 from_le16(u16 x) {
return swap16(x);
}
/** Convert 32-bit value to little-endian */ /** Convert 32-bit value to little-endian */
static inline u32 to_le32(u32 x) { return swap32(x); } static inline u32 to_le32(u32 x) {
return swap32(x);
}
/** Convert 32-bit little-endian value to host */ /** Convert 32-bit little-endian value to host */
static inline u32 from_le32(u32 x) { return swap32(x); } static inline u32 from_le32(u32 x) {
return swap32(x);
}
/** Convert 64-bit value to little-endian */ /** Convert 64-bit value to little-endian */
static inline u64 to_le64(u64 x) { return swap64(x); } static inline u64 to_le64(u64 x) {
return swap64(x);
}
/** Convert 64-bit little-endian value to host */ /** Convert 64-bit little-endian value to host */
static inline u64 from_le64(u64 x) { return swap64(x); } static inline u64 from_le64(u64 x) {
return swap64(x);
}
/** Convert 16-bit value to big-endian */ /** Convert 16-bit value to big-endian */
static inline u16 to_be16(u16 x) { return x; } static inline u16 to_be16(u16 x) {
return x;
}
/** Convert 16-bit big-endian value to host */ /** Convert 16-bit big-endian value to host */
static inline u16 from_be16(u16 x) { return x; } static inline u16 from_be16(u16 x) {
return x;
}
/** Convert 32-bit value to big-endian */ /** Convert 32-bit value to big-endian */
static inline u32 to_be32(u32 x) { return x; } static inline u32 to_be32(u32 x) {
return x;
}
/** Convert 32-bit big-endian value to host */ /** Convert 32-bit big-endian value to host */
static inline u32 from_be32(u32 x) { return x; } static inline u32 from_be32(u32 x) {
return x;
}
/** Convert 64-bit value to big-endian */ /** Convert 64-bit value to big-endian */
static inline u64 to_be64(u64 x) { return x; } static inline u64 to_be64(u64 x) {
return x;
}
/** Convert 64-bit big-endian value to host */ /** Convert 64-bit big-endian value to host */
static inline u64 from_be64(u64 x) { return x; } static inline u64 from_be64(u64 x) {
return x;
}
#endif // __LITTLE_ENDIAN__ #endif // __LITTLE_ENDIAN__

View file

@ -10,8 +10,7 @@ size_t probe_memory(void) {
u32 test_pattern = 0xA5A5A5A5; u32 test_pattern = 0xA5A5A5A5;
size_t detected = 0; size_t detected = 0;
for (size_t offset = 4096 * 16; offset < MAX_PROBE_SIZE; for (size_t offset = 4096 * 16; offset < MAX_PROBE_SIZE; offset += PROBE_STEP) {
offset += PROBE_STEP) {
addr = (volatile u32 *)(KERNBASE + offset); addr = (volatile u32 *)(KERNBASE + offset);
u32 old = *addr; u32 old = *addr;

View file

@ -43,269 +43,251 @@
#include "mini-printf.h" #include "mini-printf.h"
static int static int mini_strlen(const char *s) {
mini_strlen(const char *s) int len = 0;
{ while (s[len] != '\0') len++;
int len = 0; return len;
while (s[len] != '\0') len++;
return len;
} }
static int static int mini_itoa(long value, unsigned int radix, int uppercase, int unsig, char *buffer) {
mini_itoa(long value, unsigned int radix, int uppercase, int unsig, char *pbuffer = buffer;
char *buffer) int negative = 0;
{ int i, len;
char *pbuffer = buffer;
int negative = 0;
int i, len;
/* No support for unusual radixes. */ /* No support for unusual radixes. */
if (radix > 16) if (radix > 16)
return 0; return 0;
if (value < 0 && !unsig) { if (value < 0 && !unsig) {
negative = 1; negative = 1;
value = -value; value = -value;
} }
/* This builds the string back to front ... */ /* This builds the string back to front ... */
do { do {
int digit = value % radix; int digit = value % radix;
*(pbuffer++) = (digit < 10 ? '0' + digit : (uppercase ? 'A' : 'a') + digit - 10); *(pbuffer++) = (digit < 10 ? '0' + digit : (uppercase ? 'A' : 'a') + digit - 10);
value /= radix; value /= radix;
} while (value > 0); } while (value > 0);
if (negative) if (negative)
*(pbuffer++) = '-'; *(pbuffer++) = '-';
*(pbuffer) = '\0'; *(pbuffer) = '\0';
/* ... now we reverse it (could do it recursively but will /* ... now we reverse it (could do it recursively but will
* conserve the stack space) */ * conserve the stack space) */
len = (pbuffer - buffer); len = (pbuffer - buffer);
for (i = 0; i < len / 2; i++) { for (i = 0; i < len / 2; i++) {
char j = buffer[i]; char j = buffer[i];
buffer[i] = buffer[len-i-1]; buffer[i] = buffer[len - i - 1];
buffer[len-i-1] = j; buffer[len - i - 1] = j;
} }
return len; return len;
} }
static int static int mini_pad(char *ptr, int len, char pad_char, int pad_to, char *buffer) {
mini_pad(char* ptr, int len, char pad_char, int pad_to, char *buffer) int i;
{ int overflow = 0;
int i; char *pbuffer = buffer;
int overflow = 0; if (pad_to == 0)
char * pbuffer = buffer; pad_to = len;
if(pad_to == 0) pad_to = len; if (len > pad_to) {
if(len > pad_to) { len = pad_to;
len = pad_to; overflow = 1;
overflow = 1; }
} for (i = pad_to - len; i > 0; i--) {
for(i = pad_to - len; i > 0; i --) { *(pbuffer++) = pad_char;
*(pbuffer++) = pad_char; }
} for (i = len; i > 0; i--) {
for(i = len; i > 0; i --) { *(pbuffer++) = *(ptr++);
*(pbuffer++) = *(ptr++); }
} len = pbuffer - buffer;
len = pbuffer - buffer; if (overflow) {
if(overflow) { for (i = 0; i < 3 && pbuffer > buffer; i++) {
for (i = 0; i < 3 && pbuffer > buffer; i ++) { *(pbuffer-- - 1) = '*';
*(pbuffer-- - 1) = '*'; }
} }
} return len;
return len;
} }
struct mini_buff { struct mini_buff {
char *buffer, *pbuffer; char *buffer, *pbuffer;
unsigned int buffer_len; unsigned int buffer_len;
}; };
static int static int _puts(char *s, int len, void *buf) {
_puts(char *s, int len, void *buf) if (!buf)
{ return len;
if(!buf) return len; struct mini_buff *b = buf;
struct mini_buff *b = buf; char *p0 = b->buffer;
char * p0 = b->buffer; int i;
int i; /* Copy to buffer */
/* Copy to buffer */ for (i = 0; i < len; i++) {
for (i = 0; i < len; i++) { if (b->pbuffer == b->buffer + b->buffer_len - 1) {
if(b->pbuffer == b->buffer + b->buffer_len - 1) { break;
break; }
} *(b->pbuffer++) = s[i];
*(b->pbuffer ++) = s[i]; }
} *(b->pbuffer) = 0;
*(b->pbuffer) = 0; return b->pbuffer - p0;
return b->pbuffer - p0;
} }
#ifdef MINI_PRINTF_ENABLE_OBJECTS #ifdef MINI_PRINTF_ENABLE_OBJECTS
static int (*mini_handler) (void* data, void* obj, int ch, int lhint, char** bf) = 0; static int (*mini_handler)(void *data, void *obj, int ch, int lhint, char **bf) = 0;
static void (*mini_handler_freeor)(void* data, void*) = 0; static void (*mini_handler_freeor)(void *data, void *) = 0;
static void * mini_handler_data = 0; static void *mini_handler_data = 0;
void mini_printf_set_handler( void mini_printf_set_handler(void *data, int (*handler)(void *data, void *obj, int ch, int len_hint, char **buf),
void* data, void (*freeor)(void *data, void *buf)) {
int (*handler)(void* data, void* obj, int ch, int len_hint, char** buf), mini_handler = handler;
void (*freeor)(void* data, void* buf)) mini_handler_freeor = freeor;
{ mini_handler_data = data;
mini_handler = handler;
mini_handler_freeor = freeor;
mini_handler_data = data;
} }
#endif #endif
int int mini_vsnprintf(char *buffer, unsigned int buffer_len, const char *fmt, va_list va) {
mini_vsnprintf(char *buffer, unsigned int buffer_len, const char *fmt, va_list va) struct mini_buff b;
{ b.buffer = buffer;
struct mini_buff b; b.pbuffer = buffer;
b.buffer = buffer; b.buffer_len = buffer_len;
b.pbuffer = buffer; if (buffer_len == 0)
b.buffer_len = buffer_len; buffer = (void *)0;
if(buffer_len == 0) buffer = (void*) 0; int n = mini_vpprintf(_puts, (buffer != (void *)0) ? &b : (void *)0, fmt, va);
int n = mini_vpprintf(_puts, (buffer != (void*)0)?&b:(void*)0, fmt, va); if (buffer == (void *)0) {
if(buffer == (void*) 0) { return n;
return n; }
} return b.pbuffer - b.buffer;
return b.pbuffer - b.buffer;
} }
int int mini_vpprintf(int (*puts)(char *s, int len, void *buf), void *buf, const char *fmt, va_list va) {
mini_vpprintf(int (*puts)(char* s, int len, void* buf), void* buf, const char *fmt, va_list va) char bf[24];
{ char bf2[24];
char bf[24]; char ch;
char bf2[24];
char ch;
#ifdef MINI_PRINTF_ENABLE_OBJECTS #ifdef MINI_PRINTF_ENABLE_OBJECTS
void* obj; void *obj;
#endif #endif
if(puts == (void*)0) { if (puts == (void *)0) {
/* run puts in counting mode. */ /* run puts in counting mode. */
puts = _puts; buf = (void*)0; puts = _puts;
} buf = (void *)0;
int n = 0; }
while ((ch=*(fmt++))) { int n = 0;
int len; while ((ch = *(fmt++))) {
if (ch!='%') { int len;
len = 1; if (ch != '%') {
len = puts(&ch, len, buf); len = 1;
} else { len = puts(&ch, len, buf);
char pad_char = ' '; } else {
int pad_to = 0; char pad_char = ' ';
char l = 0; int pad_to = 0;
char *ptr; char l = 0;
char *ptr;
ch=*(fmt++); ch = *(fmt++);
/* Zero padding requested */ /* Zero padding requested */
if (ch == '0') pad_char = '0'; if (ch == '0')
while (ch >= '0' && ch <= '9') { pad_char = '0';
pad_to = pad_to * 10 + (ch - '0'); while (ch >= '0' && ch <= '9') {
ch=*(fmt++); pad_to = pad_to * 10 + (ch - '0');
} ch = *(fmt++);
if(pad_to > (signed int) sizeof(bf)) { }
pad_to = sizeof(bf); if (pad_to > (signed int)sizeof(bf)) {
} pad_to = sizeof(bf);
if (ch == 'l') { }
l = 1; if (ch == 'l') {
ch=*(fmt++); l = 1;
} ch = *(fmt++);
}
switch (ch) { switch (ch) {
case 0: case 0: goto end;
goto end; case 'u':
case 'u': case 'd':
case 'd': if (l) {
if(l) { len = mini_itoa(va_arg(va, unsigned long), 10, 0, (ch == 'u'), bf2);
len = mini_itoa(va_arg(va, unsigned long), 10, 0, (ch=='u'), bf2); } else {
} else { if (ch == 'u') {
if(ch == 'u') { len = mini_itoa((unsigned long)va_arg(va, unsigned int), 10, 0, 1, bf2);
len = mini_itoa((unsigned long) va_arg(va, unsigned int), 10, 0, 1, bf2); } else {
} else { len = mini_itoa((long)va_arg(va, int), 10, 0, 0, bf2);
len = mini_itoa((long) va_arg(va, int), 10, 0, 0, bf2); }
} }
} len = mini_pad(bf2, len, pad_char, pad_to, bf);
len = mini_pad(bf2, len, pad_char, pad_to, bf); len = puts(bf, len, buf);
len = puts(bf, len, buf); break;
break;
case 'x': case 'x':
case 'X': case 'X':
if(l) { if (l) {
len = mini_itoa(va_arg(va, unsigned long), 16, (ch=='X'), 1, bf2); len = mini_itoa(va_arg(va, unsigned long), 16, (ch == 'X'), 1, bf2);
} else { } else {
len = mini_itoa((unsigned long) va_arg(va, unsigned int), 16, (ch=='X'), 1, bf2); len = mini_itoa((unsigned long)va_arg(va, unsigned int), 16, (ch == 'X'), 1, bf2);
} }
len = mini_pad(bf2, len, pad_char, pad_to, bf); len = mini_pad(bf2, len, pad_char, pad_to, bf);
len = puts(bf, len, buf); len = puts(bf, len, buf);
break; break;
case 'c' : case 'c':
ch = (char)(va_arg(va, int)); ch = (char)(va_arg(va, int));
len = mini_pad(&ch, 1, pad_char, pad_to, bf); len = mini_pad(&ch, 1, pad_char, pad_to, bf);
len = puts(bf, len, buf); len = puts(bf, len, buf);
break; break;
case 's' : case 's':
ptr = va_arg(va, char*); ptr = va_arg(va, char *);
len = mini_strlen(ptr); len = mini_strlen(ptr);
if (pad_to > 0) { if (pad_to > 0) {
len = mini_pad(ptr, len, pad_char, pad_to, bf); len = mini_pad(ptr, len, pad_char, pad_to, bf);
len = puts(bf, len, buf); len = puts(bf, len, buf);
} else { } else {
len = puts(ptr, len, buf); len = puts(ptr, len, buf);
} }
break; break;
#ifdef MINI_PRINTF_ENABLE_OBJECTS #ifdef MINI_PRINTF_ENABLE_OBJECTS
case 'O' : /* Object by content (e.g. str) */ case 'O': /* Object by content (e.g. str) */
case 'R' : /* Object by representation (e.g. repr)*/ case 'R': /* Object by representation (e.g. repr)*/
obj = va_arg(va, void*); obj = va_arg(va, void *);
len = mini_handler(mini_handler_data, obj, ch, pad_to, &ptr); len = mini_handler(mini_handler_data, obj, ch, pad_to, &ptr);
if (pad_to > 0) { if (pad_to > 0) {
len = mini_pad(ptr, len, pad_char, pad_to, bf); len = mini_pad(ptr, len, pad_char, pad_to, bf);
len = puts(bf, len, buf); len = puts(bf, len, buf);
} else { } else {
len = puts(ptr, len, buf); len = puts(ptr, len, buf);
} }
mini_handler_freeor(mini_handler_data, ptr); mini_handler_freeor(mini_handler_data, ptr);
break; break;
#endif #endif
default: default:
len = 1; len = 1;
len = puts(&ch, len, buf); len = puts(&ch, len, buf);
break; break;
} }
} }
n = n + len; n = n + len;
} }
end: end:
return n; return n;
} }
int mini_snprintf(char *buffer, unsigned int buffer_len, const char *fmt, ...) {
int ret;
va_list va;
va_start(va, fmt);
ret = mini_vsnprintf(buffer, buffer_len, fmt, va);
va_end(va);
int return ret;
mini_snprintf(char* buffer, unsigned int buffer_len, const char *fmt, ...)
{
int ret;
va_list va;
va_start(va, fmt);
ret = mini_vsnprintf(buffer, buffer_len, fmt, va);
va_end(va);
return ret;
} }
int int mini_pprintf(int (*puts)(char *s, int len, void *buf), void *buf, const char *fmt, ...) {
mini_pprintf(int (*puts)(char*s, int len, void* buf), void* buf, const char *fmt, ...) int ret;
{ va_list va;
int ret; va_start(va, fmt);
va_list va; ret = mini_vpprintf(puts, buf, fmt, va);
va_start(va, fmt); va_end(va);
ret = mini_vpprintf(puts, buf, fmt, va);
va_end(va);
return ret; return ret;
} }

View file

@ -27,7 +27,6 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef __MINI_PRINTF__ #ifndef __MINI_PRINTF__
#define __MINI_PRINTF__ #define __MINI_PRINTF__
@ -42,32 +41,31 @@ extern "C" {
* void* arguments matching %O and %R are sent to handler as obj. * void* arguments matching %O and %R are sent to handler as obj.
* the result string created by handler at *buf is freed by freeor. * the result string created by handler at *buf is freed by freeor.
* */ * */
void mini_printf_set_handler( void mini_printf_set_handler(void *data,
void * data, /* handler returns number of chars in *buf; *buf is not NUL-terminated. */
/* handler returns number of chars in *buf; *buf is not NUL-terminated. */ int (*handler)(void *data, void *obj, int ch, int len_hint, char **buf),
int (*handler)(void* data, void* obj, int ch, int len_hint, char** buf), void (*freeor)(void *data, void *buf));
void (*freeor)(void* data, void* buf));
#endif #endif
/* String IO interface; returns number of bytes written, not including the ending NUL. /* String IO interface; returns number of bytes written, not including the ending NUL.
* Always appends a NUL at the end, therefore buffer_len shall be at least 1 in normal operation. * Always appends a NUL at the end, therefore buffer_len shall be at least 1 in normal operation.
* If buffer is NULL or buffer_len is 0, returns number of bytes to be written, not including the ending NUL. * If buffer is NULL or buffer_len is 0, returns number of bytes to be written, not including the ending NUL.
*/ */
int mini_vsnprintf(char* buffer, unsigned int buffer_len, const char *fmt, va_list va); int mini_vsnprintf(char *buffer, unsigned int buffer_len, const char *fmt, va_list va);
int mini_snprintf(char* buffer, unsigned int buffer_len, const char *fmt, ...); int mini_snprintf(char *buffer, unsigned int buffer_len, const char *fmt, ...);
/* Stream IO interface; returns number of bytes written. /* Stream IO interface; returns number of bytes written.
* If puts is NULL, number of bytes to be written. * If puts is NULL, number of bytes to be written.
* puts shall return number of bytes written. * puts shall return number of bytes written.
*/ */
int mini_vpprintf(int (*puts)(char* s, int len, void* buf), void* buf, const char *fmt, va_list va); int mini_vpprintf(int (*puts)(char *s, int len, void *buf), void *buf, const char *fmt, va_list va);
int mini_pprintf(int (*puts)(char*s, int len, void* buf), void* buf, const char *fmt, ...); int mini_pprintf(int (*puts)(char *s, int len, void *buf), void *buf, const char *fmt, ...);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#define vsnprintf mini_vsnprintf #define vsnprintf mini_vsnprintf
#define snprintf mini_snprintf #define snprintf mini_snprintf
#endif #endif

View file

@ -39,7 +39,9 @@ static inline u64 read_tp() {
} }
/** Write thread pointer */ /** Write thread pointer */
static inline void write_tp(u64 x) { asm volatile("mv tp, %0" : : "r"(x)); } static inline void write_tp(u64 x) {
asm volatile("mv tp, %0" : : "r"(x));
}
/** /**
* Read the value of the sstatus register. * Read the value of the sstatus register.
@ -60,10 +62,14 @@ static inline void w_sstatus(u64 x) {
} }
/** Enable device interrupts */ /** Enable device interrupts */
static inline void intr_on() { w_sstatus(r_sstatus() | SSTATUS_SIE); } static inline void intr_on() {
w_sstatus(r_sstatus() | SSTATUS_SIE);
}
/** Disable device interrupts */ /** Disable device interrupts */
static inline void intr_off() { w_sstatus(r_sstatus() & ~SSTATUS_SIE); } static inline void intr_off() {
w_sstatus(r_sstatus() & ~SSTATUS_SIE);
}
/** Are device interrupts enabled? */ /** Are device interrupts enabled? */
static inline int intr_get() { static inline int intr_get() {

View file

@ -1,7 +1,9 @@
/* QEMU memory maps a UART device here. */ /* QEMU memory maps a UART device here. */
#define UART_BASE ((volatile char *)0x10000000) #define UART_BASE ((volatile char *)0x10000000)
void uart_putc(char c) { *UART_BASE = c; } void uart_putc(char c) {
*UART_BASE = c;
}
void uart_puts(const char *s) { void uart_puts(const char *s) {
while (*s) uart_putc(*s++); while (*s) uart_putc(*s++);

View file

@ -1,6 +1,6 @@
#include <config.h> #include <config.h>
#include <spinlock.h>
#include <riscv.h> #include <riscv.h>
#include <spinlock.h>
#include <stdint.h> #include <stdint.h>
typedef enum { typedef enum {

View file

@ -1,9 +1,9 @@
#include <config.h> #include <config.h>
#include <spinlock.h>
#include <kalloc.h> #include <kalloc.h>
#include <memory.h> #include <memory.h>
#include <proc.h> #include <proc.h>
#include <riscv.h> #include <riscv.h>
#include <spinlock.h>
#include <stdint.h> #include <stdint.h>
#include <uart.h> #include <uart.h>