Compare commits

..

No commits in common. "f1fd7de79fb2002d7402548d9948a574e2b16f8f" and "3a476fa1957653ba8b4057be9165b7f2d8aede5f" have entirely different histories.

11 changed files with 297 additions and 320 deletions

View file

@ -53,9 +53,6 @@ clean:
rm -f *.o *.elf *.d lib/*.o lib/*.d rm -f *.o *.elf *.d lib/*.o lib/*.d
find . -type f -name '*.[od]' -exec rm -f {} + find . -type f -name '*.[od]' -exec rm -f {} +
format:
find . -type f -name '*.[ch]' -exec clang-format -i {} +
-include *.d -include *.d
.PHONY: all .PHONY: all

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 <spinlock.h>
#include <stdint.h>
#include <string.h> #include <string.h>
#include <stdint.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,12 +52,24 @@ int kvsnprintf(char *buf, size_t size, const char *fmt, va_list args) {
fmt++; // skip '%' fmt++; // skip '%'
switch (*fmt) { switch (*fmt) {
case 's': append_str(&p, &remaining, va_arg(args, const char *)); break; case 's':
case 'd': append_int(&p, &remaining, va_arg(args, int), 10); break; append_str(&p, &remaining, va_arg(args, const char *));
case 'x': append_int(&p, &remaining, va_arg(args, unsigned int), 16); break; break;
case 'c': append_char(&p, &remaining, (char)va_arg(args, int)); break; case 'd':
case '%': append_char(&p, &remaining, '%'); break; append_int(&p, &remaining, va_arg(args, int), 10);
default: append_char(&p, &remaining, '?'); break; 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,133 +4,88 @@
#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) { static inline u16 swap16(u16 x) { return (x >> 8) | (x << 8); }
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) | ((x << 8) & 0x00ff0000) | ((x << 24) & 0xff000000); return ((x >> 24) & 0x000000ff) | ((x >> 8) & 0x0000ff00) |
((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) | ((x >> 40) & 0x000000000000ff00ULL) | return ((x >> 56) & 0x00000000000000ffULL) |
((x >> 24) & 0x0000000000ff0000ULL) | ((x >> 8) & 0x00000000ff000000ULL) | ((x >> 40) & 0x000000000000ff00ULL) |
((x << 8) & 0x000000ff00000000ULL) | ((x << 24) & 0x0000ff0000000000ULL) | ((x >> 24) & 0x0000000000ff0000ULL) |
((x << 40) & 0x00ff000000000000ULL) | ((x << 56) & 0xff00000000000000ULL); ((x >> 8) & 0x00000000ff000000ULL) |
((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) { static inline u16 to_le16(u16 x) { return 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) { static inline u16 from_le16(u16 x) { return x; }
return x;
}
/** Convert 32-bit value to little-endian */ /** Convert 32-bit value to little-endian */
static inline u32 to_le32(u32 x) { static inline u32 to_le32(u32 x) { return 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) { static inline u32 from_le32(u32 x) { return x; }
return x;
}
/** Convert 64-bit value to little-endian */ /** Convert 64-bit value to little-endian */
static inline u64 to_le64(u64 x) { static inline u64 to_le64(u64 x) { return 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) { static inline u64 from_le64(u64 x) { return x; }
return x;
}
/** Convert 16-bit value to big-endian */ /** Convert 16-bit value to big-endian */
static inline u16 to_be16(u16 x) { static inline u16 to_be16(u16 x) { return swap16(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) { static inline u16 from_be16(u16 x) { return swap16(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) { static inline u32 to_be32(u32 x) { return swap32(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) { static inline u32 from_be32(u32 x) { return swap32(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) { static inline u64 to_be64(u64 x) { return swap64(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) { static inline u64 from_be64(u64 x) { return swap64(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) { static inline u16 to_le16(u16 x) { return swap16(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) { static inline u16 from_le16(u16 x) { return swap16(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) { static inline u32 to_le32(u32 x) { return swap32(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) { static inline u32 from_le32(u32 x) { return swap32(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) { static inline u64 to_le64(u64 x) { return swap64(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) { static inline u64 from_le64(u64 x) { return swap64(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) { static inline u16 to_be16(u16 x) { return 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) { static inline u16 from_be16(u16 x) { return x; }
return x;
}
/** Convert 32-bit value to big-endian */ /** Convert 32-bit value to big-endian */
static inline u32 to_be32(u32 x) { static inline u32 to_be32(u32 x) { return 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) { static inline u32 from_be32(u32 x) { return x; }
return x;
}
/** Convert 64-bit value to big-endian */ /** Convert 64-bit value to big-endian */
static inline u64 to_be64(u64 x) { static inline u64 to_be64(u64 x) { return 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) { static inline u64 from_be64(u64 x) { return x; }
return x;
}
#endif // __LITTLE_ENDIAN__ #endif // __LITTLE_ENDIAN__

View file

@ -10,7 +10,8 @@ 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; offset += PROBE_STEP) { for (size_t offset = 4096 * 16; offset < MAX_PROBE_SIZE;
offset += PROBE_STEP) {
addr = (volatile u32 *)(KERNBASE + offset); addr = (volatile u32 *)(KERNBASE + offset);
u32 old = *addr; u32 old = *addr;

View file

@ -43,251 +43,269 @@
#include "mini-printf.h" #include "mini-printf.h"
static int mini_strlen(const char *s) { static int
int len = 0; mini_strlen(const char *s)
while (s[len] != '\0') len++; {
return len; int len = 0;
while (s[len] != '\0') len++;
return len;
} }
static int mini_itoa(long value, unsigned int radix, int uppercase, int unsig, char *buffer) { static int
char *pbuffer = buffer; mini_itoa(long value, unsigned int radix, int uppercase, int unsig,
int negative = 0; char *buffer)
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 mini_pad(char *ptr, int len, char pad_char, int pad_to, char *buffer) { static int
int i; mini_pad(char* ptr, int len, char pad_char, int pad_to, char *buffer)
int overflow = 0; {
char *pbuffer = buffer; int i;
if (pad_to == 0) int overflow = 0;
pad_to = len; char * pbuffer = buffer;
if (len > pad_to) { if(pad_to == 0) pad_to = len;
len = pad_to; if(len > pad_to) {
overflow = 1; len = pad_to;
} overflow = 1;
for (i = pad_to - len; i > 0; i--) { }
*(pbuffer++) = pad_char; for(i = pad_to - len; i > 0; i --) {
} *(pbuffer++) = pad_char;
for (i = len; i > 0; i--) { }
*(pbuffer++) = *(ptr++); for(i = len; i > 0; i --) {
} *(pbuffer++) = *(ptr++);
len = pbuffer - buffer; }
if (overflow) { len = pbuffer - buffer;
for (i = 0; i < 3 && pbuffer > buffer; i++) { if(overflow) {
*(pbuffer-- - 1) = '*'; for (i = 0; i < 3 && pbuffer > buffer; i ++) {
} *(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 _puts(char *s, int len, void *buf) { static int
if (!buf) _puts(char *s, int len, void *buf)
return len; {
struct mini_buff *b = buf; if(!buf) return len;
char *p0 = b->buffer; struct mini_buff *b = buf;
int i; char * p0 = b->buffer;
/* Copy to buffer */ int i;
for (i = 0; i < len; i++) { /* Copy to buffer */
if (b->pbuffer == b->buffer + b->buffer_len - 1) { for (i = 0; i < len; i++) {
break; if(b->pbuffer == b->buffer + b->buffer_len - 1) {
} break;
*(b->pbuffer++) = s[i]; }
} *(b->pbuffer ++) = s[i];
*(b->pbuffer) = 0; }
return b->pbuffer - p0; *(b->pbuffer) = 0;
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 *data, int (*handler)(void *data, void *obj, int ch, int len_hint, char **buf), void mini_printf_set_handler(
void (*freeor)(void *data, void *buf)) { void* data,
mini_handler = handler; int (*handler)(void* data, void* obj, int ch, int len_hint, char** buf),
mini_handler_freeor = freeor; void (*freeor)(void* data, void* buf))
mini_handler_data = data; {
mini_handler = handler;
mini_handler_freeor = freeor;
mini_handler_data = data;
} }
#endif #endif
int mini_vsnprintf(char *buffer, unsigned int buffer_len, const char *fmt, va_list va) { int
struct mini_buff b; mini_vsnprintf(char *buffer, unsigned int buffer_len, const char *fmt, va_list va)
b.buffer = buffer; {
b.pbuffer = buffer; struct mini_buff b;
b.buffer_len = buffer_len; b.buffer = buffer;
if (buffer_len == 0) b.pbuffer = buffer;
buffer = (void *)0; b.buffer_len = buffer_len;
int n = mini_vpprintf(_puts, (buffer != (void *)0) ? &b : (void *)0, fmt, va); if(buffer_len == 0) buffer = (void*) 0;
if (buffer == (void *)0) { int n = mini_vpprintf(_puts, (buffer != (void*)0)?&b:(void*)0, fmt, va);
return n; if(buffer == (void*) 0) {
} return n;
return b.pbuffer - b.buffer; }
return b.pbuffer - b.buffer;
} }
int mini_vpprintf(int (*puts)(char *s, int len, void *buf), void *buf, const char *fmt, va_list va) { int
char bf[24]; mini_vpprintf(int (*puts)(char* s, int len, void* buf), void* buf, const char *fmt, va_list va)
char bf2[24]; {
char ch; char bf[24];
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; puts = _puts; buf = (void*)0;
buf = (void *)0; }
} int n = 0;
int n = 0; while ((ch=*(fmt++))) {
while ((ch = *(fmt++))) { int len;
int len; if (ch!='%') {
if (ch != '%') { len = 1;
len = 1; len = puts(&ch, len, buf);
len = puts(&ch, len, buf); } else {
} else { char pad_char = ' ';
char pad_char = ' '; int pad_to = 0;
int pad_to = 0; char l = 0;
char l = 0; char *ptr;
char *ptr;
ch = *(fmt++); ch=*(fmt++);
/* Zero padding requested */ /* Zero padding requested */
if (ch == '0') if (ch == '0') pad_char = '0';
pad_char = '0'; while (ch >= '0' && ch <= '9') {
while (ch >= '0' && ch <= '9') { pad_to = pad_to * 10 + (ch - '0');
pad_to = pad_to * 10 + (ch - '0'); ch=*(fmt++);
ch = *(fmt++); }
} if(pad_to > (signed int) sizeof(bf)) {
if (pad_to > (signed int)sizeof(bf)) { pad_to = sizeof(bf);
pad_to = sizeof(bf); }
} if (ch == 'l') {
if (ch == 'l') { l = 1;
l = 1; ch=*(fmt++);
ch = *(fmt++); }
}
switch (ch) { switch (ch) {
case 0: goto end; case 0:
case 'u': goto end;
case 'd': case 'u':
if (l) { case 'd':
len = mini_itoa(va_arg(va, unsigned long), 10, 0, (ch == 'u'), bf2); if(l) {
} else { len = mini_itoa(va_arg(va, unsigned long), 10, 0, (ch=='u'), bf2);
if (ch == 'u') { } else {
len = mini_itoa((unsigned long)va_arg(va, unsigned int), 10, 0, 1, bf2); if(ch == 'u') {
} else { len = mini_itoa((unsigned long) va_arg(va, unsigned int), 10, 0, 1, bf2);
len = mini_itoa((long)va_arg(va, int), 10, 0, 0, bf2); } else {
} len = mini_itoa((long) va_arg(va, int), 10, 0, 0, bf2);
} }
len = mini_pad(bf2, len, pad_char, pad_to, bf); }
len = puts(bf, len, buf); len = mini_pad(bf2, len, pad_char, pad_to, bf);
break; len = puts(bf, len, buf);
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);
return ret; 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);
return ret;
} }
int mini_pprintf(int (*puts)(char *s, int len, void *buf), void *buf, const char *fmt, ...) { int
int ret; mini_pprintf(int (*puts)(char*s, int len, void* buf), void* buf, const char *fmt, ...)
va_list va; {
va_start(va, fmt); int ret;
ret = mini_vpprintf(puts, buf, fmt, va); va_list va;
va_end(va); va_start(va, fmt);
ret = mini_vpprintf(puts, buf, fmt, va);
va_end(va);
return ret; return ret;
} }

View file

@ -27,6 +27,7 @@
* 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__
@ -41,31 +42,32 @@ 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 *data, void mini_printf_set_handler(
/* handler returns number of chars in *buf; *buf is not NUL-terminated. */ void * data,
int (*handler)(void *data, void *obj, int ch, int len_hint, char **buf), /* handler returns number of chars in *buf; *buf is not NUL-terminated. */
void (*freeor)(void *data, void *buf)); int (*handler)(void* data, void* obj, int ch, int len_hint, char** 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,9 +39,7 @@ static inline u64 read_tp() {
} }
/** Write thread pointer */ /** Write thread pointer */
static inline void write_tp(u64 x) { static inline void write_tp(u64 x) { asm volatile("mv tp, %0" : : "r"(x)); }
asm volatile("mv tp, %0" : : "r"(x));
}
/** /**
* Read the value of the sstatus register. * Read the value of the sstatus register.
@ -62,14 +60,10 @@ static inline void w_sstatus(u64 x) {
} }
/** Enable device interrupts */ /** Enable device interrupts */
static inline void intr_on() { static inline void intr_on() { w_sstatus(r_sstatus() | SSTATUS_SIE); }
w_sstatus(r_sstatus() | SSTATUS_SIE);
}
/** Disable device interrupts */ /** Disable device interrupts */
static inline void intr_off() { static inline void intr_off() { w_sstatus(r_sstatus() & ~SSTATUS_SIE); }
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,9 +1,7 @@
/* 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) { void uart_putc(char c) { *UART_BASE = 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 <riscv.h>
#include <spinlock.h> #include <spinlock.h>
#include <riscv.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>