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,13 +43,18 @@
#include "mini-printf.h" #include "mini-printf.h"
static int mini_strlen(const char *s) { static int
mini_strlen(const char *s)
{
int len = 0; int len = 0;
while (s[len] != '\0') len++; while (s[len] != '\0') len++;
return len; return len;
} }
static int mini_itoa(long value, unsigned int radix, int uppercase, int unsig, char *buffer) { static int
mini_itoa(long value, unsigned int radix, int uppercase, int unsig,
char *buffer)
{
char *pbuffer = buffer; char *pbuffer = buffer;
int negative = 0; int negative = 0;
int i, len; int i, len;
@ -87,12 +92,13 @@ static int mini_itoa(long value, unsigned int radix, int uppercase, int unsig, c
return len; return len;
} }
static int mini_pad(char *ptr, int len, char pad_char, int pad_to, char *buffer) { static int
mini_pad(char* ptr, int len, char pad_char, int pad_to, char *buffer)
{
int i; int i;
int overflow = 0; int overflow = 0;
char * pbuffer = buffer; char * pbuffer = buffer;
if (pad_to == 0) if(pad_to == 0) pad_to = len;
pad_to = len;
if(len > pad_to) { if(len > pad_to) {
len = pad_to; len = pad_to;
overflow = 1; overflow = 1;
@ -117,9 +123,10 @@ struct mini_buff {
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; {
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;
@ -139,21 +146,25 @@ static int (*mini_handler)(void *data, void *obj, int ch, int lhint, char **bf)
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,
int (*handler)(void* data, void* obj, int ch, int len_hint, char** buf),
void (*freeor)(void* data, void* buf))
{
mini_handler = handler; mini_handler = handler;
mini_handler_freeor = freeor; mini_handler_freeor = freeor;
mini_handler_data = data; mini_handler_data = data;
} }
#endif #endif
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)
{
struct mini_buff b; struct mini_buff b;
b.buffer = buffer; b.buffer = buffer;
b.pbuffer = buffer; b.pbuffer = buffer;
b.buffer_len = buffer_len; b.buffer_len = buffer_len;
if (buffer_len == 0) if(buffer_len == 0) buffer = (void*) 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;
@ -161,7 +172,9 @@ int mini_vsnprintf(char *buffer, unsigned int buffer_len, const char *fmt, va_li
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
mini_vpprintf(int (*puts)(char* s, int len, void* buf), void* buf, const char *fmt, va_list va)
{
char bf[24]; char bf[24];
char bf2[24]; char bf2[24];
char ch; char ch;
@ -170,8 +183,7 @@ int mini_vpprintf(int (*puts)(char *s, int len, void *buf), void *buf, const cha
#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++))) {
@ -188,8 +200,7 @@ int mini_vpprintf(int (*puts)(char *s, int len, void *buf), void *buf, const cha
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++);
@ -203,7 +214,8 @@ int mini_vpprintf(int (*puts)(char *s, int len, void *buf), void *buf, const cha
} }
switch (ch) { switch (ch) {
case 0: goto end; case 0:
goto end;
case 'u': case 'u':
case 'd': case 'd':
if(l) { if(l) {
@ -272,7 +284,10 @@ end:
return n; return n;
} }
int mini_snprintf(char *buffer, unsigned int buffer_len, const char *fmt, ...) {
int
mini_snprintf(char* buffer, unsigned int buffer_len, const char *fmt, ...)
{
int ret; int ret;
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
@ -282,7 +297,9 @@ int mini_snprintf(char *buffer, unsigned int buffer_len, const char *fmt, ...) {
return ret; return ret;
} }
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, ...)
{
int ret; int ret;
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
@ -291,3 +308,4 @@ int mini_pprintf(int (*puts)(char *s, int len, void *buf), void *buf, const char
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,7 +42,8 @@ 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(
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));

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>