ringbuffer, guid, iovec stuff, and mmio
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
#include <zinnia/guid.h>
|
||||
#include <zinnia/print.h>
|
||||
#include <string.h>
|
||||
|
||||
bool guid_is_zero(const struct guid* guid) {
|
||||
static const struct guid zero = {};
|
||||
return guid_equal(guid, &zero);
|
||||
}
|
||||
|
||||
bool guid_equal(const struct guid* a, const struct guid* b) {
|
||||
return memcmp(a, b, sizeof(struct guid)) == 0;
|
||||
}
|
||||
|
||||
void guid_format(const struct guid* guid, char out[static GUID_STR_LEN]) {
|
||||
ksnprintf(
|
||||
out,
|
||||
GUID_STR_LEN,
|
||||
"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
||||
guid->time_low,
|
||||
guid->time_mid,
|
||||
guid->time_hi_version,
|
||||
guid->clock_seq[0],
|
||||
guid->clock_seq[1],
|
||||
guid->node[0],
|
||||
guid->node[1],
|
||||
guid->node[2],
|
||||
guid->node[3],
|
||||
guid->node[4],
|
||||
guid->node[5]
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#include <zinnia/iovec.h>
|
||||
#include <zinnia/usercopy.h>
|
||||
#include <zinnia/utils.h>
|
||||
#include <string.h>
|
||||
|
||||
// Copies between `buf` and the iterator. If `to_iter` is set, data flows from `buf` into the iovecs.
|
||||
static errno_t iovec_iter_copy(iovec_iter_t* iter, void* buf, size_t len, bool to_iter, size_t* out_copied) {
|
||||
size_t progress = 0;
|
||||
errno_t status = 0;
|
||||
|
||||
while (progress < len && iter->current_idx < iter->iovecs_len) {
|
||||
iovec_t* vec = &iter->iovecs[iter->current_idx];
|
||||
size_t remaining = vec->len - iter->current_offset;
|
||||
if (remaining == 0) {
|
||||
iter->current_idx++;
|
||||
iter->current_offset = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t chunk = MIN(remaining, len - progress);
|
||||
void* ptr = (void*)(vec->base + iter->current_offset);
|
||||
uint8_t* kbuf = (uint8_t*)buf + progress;
|
||||
|
||||
if (iter->user) {
|
||||
bool ok = to_iter ? usercopy_write((__user void*)ptr, kbuf, chunk)
|
||||
: usercopy_read(kbuf, (const __user void*)ptr, chunk);
|
||||
if (!ok) {
|
||||
status = EFAULT;
|
||||
break;
|
||||
}
|
||||
} else if (to_iter) {
|
||||
memcpy(ptr, kbuf, chunk);
|
||||
} else {
|
||||
memcpy(kbuf, ptr, chunk);
|
||||
}
|
||||
|
||||
progress += chunk;
|
||||
iter->current_offset += chunk;
|
||||
iter->total_offset += chunk;
|
||||
}
|
||||
|
||||
if (out_copied)
|
||||
*out_copied = progress;
|
||||
return status;
|
||||
}
|
||||
|
||||
errno_t iovec_iter_copy_to(iovec_iter_t* iter, const void* src, size_t len, size_t* out_copied) {
|
||||
return iovec_iter_copy(iter, (void*)src, len, true, out_copied);
|
||||
}
|
||||
|
||||
errno_t iovec_iter_copy_from(iovec_iter_t* iter, void* dst, size_t len, size_t* out_copied) {
|
||||
return iovec_iter_copy(iter, dst, len, false, out_copied);
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
# Freestanding libc functions that are needed in the kernel
|
||||
|
||||
kernel_sources += files(
|
||||
'guid.c',
|
||||
'iovec.c',
|
||||
'radix.c',
|
||||
'ringbuf.c',
|
||||
'runtime.c',
|
||||
'stdlib.c',
|
||||
'string.c',
|
||||
|
||||
@@ -0,0 +1,255 @@
|
||||
#include <zinnia/alloc.h>
|
||||
#include <zinnia/radix.h>
|
||||
|
||||
static bool covers(const struct radix_node* node, uint64_t index) {
|
||||
return node->shift + RADIX_BITS >= 64 || (index >> (node->shift + RADIX_BITS)) == 0;
|
||||
}
|
||||
|
||||
static size_t slot_of(const struct radix_node* node, uint64_t index) {
|
||||
return (index >> node->shift) & (RADIX_SLOTS - 1);
|
||||
}
|
||||
|
||||
static struct radix_node* node_alloc(uint8_t shift, struct radix_node* parent, uint8_t offset) {
|
||||
struct radix_node* node = mem_alloc(sizeof(struct radix_node), 0);
|
||||
if (!node)
|
||||
return nullptr;
|
||||
node->shift = shift;
|
||||
node->parent = parent;
|
||||
node->offset = offset;
|
||||
return node;
|
||||
}
|
||||
|
||||
static void free_subtree(struct radix_node* node) {
|
||||
if (node->shift > 0) {
|
||||
for (size_t i = 0; i < RADIX_SLOTS; i++) {
|
||||
if (node->slots[i])
|
||||
free_subtree(node->slots[i]);
|
||||
}
|
||||
}
|
||||
mem_free(node);
|
||||
}
|
||||
|
||||
void radix_init(struct radix_tree* tree) {
|
||||
tree->root = nullptr;
|
||||
}
|
||||
|
||||
void radix_destroy(struct radix_tree* tree) {
|
||||
if (tree->root)
|
||||
free_subtree(tree->root);
|
||||
tree->root = nullptr;
|
||||
}
|
||||
|
||||
bool radix_empty(struct radix_tree* tree) {
|
||||
return tree->root == nullptr;
|
||||
}
|
||||
|
||||
static struct radix_node* leaf_of(struct radix_tree* tree, uint64_t index) {
|
||||
struct radix_node* node = tree->root;
|
||||
if (!node || !covers(node, index))
|
||||
return nullptr;
|
||||
while (node->shift > 0) {
|
||||
node = node->slots[slot_of(node, index)];
|
||||
if (!node)
|
||||
return nullptr;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
void* radix_lookup(struct radix_tree* tree, uint64_t index) {
|
||||
struct radix_node* leaf = leaf_of(tree, index);
|
||||
return leaf ? leaf->slots[slot_of(leaf, index)] : nullptr;
|
||||
}
|
||||
|
||||
void** radix_slot(struct radix_tree* tree, uint64_t index) {
|
||||
struct radix_node* leaf = leaf_of(tree, index);
|
||||
return leaf ? &leaf->slots[slot_of(leaf, index)] : nullptr;
|
||||
}
|
||||
|
||||
static errno_t grow(struct radix_tree* tree, uint64_t index) {
|
||||
if (!tree->root) {
|
||||
uint8_t shift = 0;
|
||||
while (shift + RADIX_BITS < 64 && (index >> (shift + RADIX_BITS)) != 0)
|
||||
shift += RADIX_BITS;
|
||||
tree->root = node_alloc(shift, nullptr, 0);
|
||||
return tree->root ? 0 : ENOMEM;
|
||||
}
|
||||
|
||||
while (!covers(tree->root, index)) {
|
||||
struct radix_node* old = tree->root;
|
||||
struct radix_node* top = node_alloc(old->shift + RADIX_BITS, nullptr, 0);
|
||||
if (!top)
|
||||
return ENOMEM;
|
||||
top->slots[0] = old;
|
||||
top->count = 1;
|
||||
for (size_t t = 0; t < RADIX_NUM_TAGS; t++) {
|
||||
if (old->tags[t])
|
||||
top->tags[t] = 1;
|
||||
}
|
||||
old->parent = top;
|
||||
old->offset = 0;
|
||||
tree->root = top;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
errno_t radix_insert(struct radix_tree* tree, uint64_t index, void* value) {
|
||||
errno_t status = grow(tree, index);
|
||||
if (status)
|
||||
return status;
|
||||
|
||||
struct radix_node* node = tree->root;
|
||||
while (node->shift > 0) {
|
||||
const size_t slot = slot_of(node, index);
|
||||
struct radix_node* child = node->slots[slot];
|
||||
if (!child) {
|
||||
child = node_alloc(node->shift - RADIX_BITS, node, slot);
|
||||
if (!child)
|
||||
return ENOMEM;
|
||||
node->slots[slot] = child;
|
||||
node->count++;
|
||||
}
|
||||
node = child;
|
||||
}
|
||||
|
||||
const size_t slot = slot_of(node, index);
|
||||
if (node->slots[slot])
|
||||
return EEXIST;
|
||||
node->slots[slot] = value;
|
||||
node->count++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void clear_tag_upwards(struct radix_node* node, size_t slot, enum radix_tag tag) {
|
||||
while (node) {
|
||||
node->tags[tag] &= ~((uint64_t)1 << slot);
|
||||
if (node->tags[tag])
|
||||
return;
|
||||
slot = node->offset;
|
||||
node = node->parent;
|
||||
}
|
||||
}
|
||||
|
||||
void* radix_remove(struct radix_tree* tree, uint64_t index) {
|
||||
struct radix_node* node = leaf_of(tree, index);
|
||||
if (!node)
|
||||
return nullptr;
|
||||
|
||||
size_t slot = slot_of(node, index);
|
||||
void* value = node->slots[slot];
|
||||
if (!value)
|
||||
return nullptr;
|
||||
|
||||
for (size_t t = 0; t < RADIX_NUM_TAGS; t++) {
|
||||
if (node->tags[t] & ((uint64_t)1 << slot))
|
||||
clear_tag_upwards(node, slot, t);
|
||||
}
|
||||
|
||||
node->slots[slot] = nullptr;
|
||||
node->count--;
|
||||
while (node->count == 0) {
|
||||
struct radix_node* parent = node->parent;
|
||||
if (!parent) {
|
||||
tree->root = nullptr;
|
||||
mem_free(node);
|
||||
break;
|
||||
}
|
||||
slot = node->offset;
|
||||
parent->slots[slot] = nullptr;
|
||||
parent->count--;
|
||||
mem_free(node);
|
||||
node = parent;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
void radix_tag_set(struct radix_tree* tree, uint64_t index, enum radix_tag tag) {
|
||||
struct radix_node* node = leaf_of(tree, index);
|
||||
if (!node || !node->slots[slot_of(node, index)])
|
||||
return;
|
||||
size_t slot = slot_of(node, index);
|
||||
while (node) {
|
||||
const uint64_t bit = (uint64_t)1 << slot;
|
||||
if (node->tags[tag] & bit)
|
||||
return;
|
||||
node->tags[tag] |= bit;
|
||||
slot = node->offset;
|
||||
node = node->parent;
|
||||
}
|
||||
}
|
||||
|
||||
void radix_tag_clear(struct radix_tree* tree, uint64_t index, enum radix_tag tag) {
|
||||
struct radix_node* node = leaf_of(tree, index);
|
||||
if (!node)
|
||||
return;
|
||||
const size_t slot = slot_of(node, index);
|
||||
if (node->tags[tag] & ((uint64_t)1 << slot))
|
||||
clear_tag_upwards(node, slot, tag);
|
||||
}
|
||||
|
||||
bool radix_tag_get(struct radix_tree* tree, uint64_t index, enum radix_tag tag) {
|
||||
struct radix_node* node = leaf_of(tree, index);
|
||||
return node && (node->tags[tag] & ((uint64_t)1 << slot_of(node, index)));
|
||||
}
|
||||
|
||||
bool radix_tagged(struct radix_tree* tree, enum radix_tag tag) {
|
||||
return tree->root && tree->root->tags[tag];
|
||||
}
|
||||
|
||||
struct gang {
|
||||
size_t max;
|
||||
size_t found;
|
||||
void** out;
|
||||
uint64_t* out_index;
|
||||
int tag;
|
||||
};
|
||||
|
||||
static void gang_walk(struct radix_node* node, uint64_t base, uint64_t first, struct gang* g) {
|
||||
size_t start = 0;
|
||||
if (first > base) {
|
||||
const uint64_t delta = (first - base) >> node->shift;
|
||||
if (delta >= RADIX_SLOTS)
|
||||
return;
|
||||
start = delta;
|
||||
}
|
||||
|
||||
for (size_t i = start; i < RADIX_SLOTS && g->found < g->max; i++) {
|
||||
if (!node->slots[i])
|
||||
continue;
|
||||
if (g->tag >= 0 && !(node->tags[g->tag] & ((uint64_t)1 << i)))
|
||||
continue;
|
||||
const uint64_t index = base + ((uint64_t)i << node->shift);
|
||||
if (node->shift == 0) {
|
||||
if (g->out)
|
||||
g->out[g->found] = node->slots[i];
|
||||
if (g->out_index)
|
||||
g->out_index[g->found] = index;
|
||||
g->found++;
|
||||
} else {
|
||||
gang_walk(node->slots[i], index, first > index ? first : index, g);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static size_t gang_lookup(struct radix_tree* tree, uint64_t first, int tag, size_t max, void** out, uint64_t* idx) {
|
||||
struct radix_node* root = tree->root;
|
||||
if (!root || max == 0 || !covers(root, first))
|
||||
return 0;
|
||||
struct gang g = {.max = max, .out = out, .out_index = idx, .tag = tag};
|
||||
gang_walk(root, 0, first, &g);
|
||||
return g.found;
|
||||
}
|
||||
|
||||
size_t radix_gang_lookup(struct radix_tree* tree, uint64_t first, size_t max, void** out, uint64_t* out_index) {
|
||||
return gang_lookup(tree, first, -1, max, out, out_index);
|
||||
}
|
||||
|
||||
size_t radix_gang_lookup_tag(
|
||||
struct radix_tree* tree,
|
||||
uint64_t first,
|
||||
enum radix_tag tag,
|
||||
size_t max,
|
||||
void** out,
|
||||
uint64_t* out_index
|
||||
) {
|
||||
return gang_lookup(tree, first, tag, max, out, out_index);
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
#include <zinnia/alloc.h>
|
||||
#include <zinnia/ringbuf.h>
|
||||
#include <zinnia/utils.h>
|
||||
#include <string.h>
|
||||
|
||||
errno_t ringbuf_init(struct ringbuf* rb, size_t capacity) {
|
||||
rb->data = mem_alloc(capacity, ALLOC_NOZERO);
|
||||
if (!rb->data)
|
||||
return ENOMEM;
|
||||
rb->capacity = capacity;
|
||||
rb->head = 0;
|
||||
rb->len = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ringbuf_fini(struct ringbuf* rb) {
|
||||
mem_free(rb->data);
|
||||
rb->data = nullptr;
|
||||
rb->capacity = 0;
|
||||
rb->len = 0;
|
||||
}
|
||||
|
||||
static size_t tail(const struct ringbuf* rb) {
|
||||
return (rb->head + rb->len) % rb->capacity;
|
||||
}
|
||||
|
||||
static size_t write_chunk(const struct ringbuf* rb, size_t len) {
|
||||
return MIN(len, MIN(ringbuf_space(rb), rb->capacity - tail(rb)));
|
||||
}
|
||||
|
||||
static size_t read_chunk(const struct ringbuf* rb, size_t len) {
|
||||
return MIN(len, MIN(rb->len, rb->capacity - rb->head));
|
||||
}
|
||||
|
||||
static void advance_read(struct ringbuf* rb, size_t len) {
|
||||
rb->head = (rb->head + len) % rb->capacity;
|
||||
rb->len -= len;
|
||||
if (rb->len == 0)
|
||||
rb->head = 0;
|
||||
}
|
||||
|
||||
size_t ringbuf_write(struct ringbuf* rb, const void* src, size_t len) {
|
||||
size_t done = 0;
|
||||
size_t chunk;
|
||||
while ((chunk = write_chunk(rb, len - done))) {
|
||||
memcpy(rb->data + tail(rb), (const uint8_t*)src + done, chunk);
|
||||
rb->len += chunk;
|
||||
done += chunk;
|
||||
}
|
||||
return done;
|
||||
}
|
||||
|
||||
size_t ringbuf_read(struct ringbuf* rb, void* dst, size_t len) {
|
||||
size_t done = 0;
|
||||
size_t chunk;
|
||||
while ((chunk = read_chunk(rb, len - done))) {
|
||||
memcpy((uint8_t*)dst + done, rb->data + rb->head, chunk);
|
||||
advance_read(rb, chunk);
|
||||
done += chunk;
|
||||
}
|
||||
return done;
|
||||
}
|
||||
|
||||
errno_t ringbuf_write_iter(struct ringbuf* rb, iovec_iter_t* iter, size_t len, size_t* out_written) {
|
||||
size_t done = 0;
|
||||
errno_t status = 0;
|
||||
size_t chunk;
|
||||
while (!status && (chunk = write_chunk(rb, len - done))) {
|
||||
size_t copied;
|
||||
status = iovec_iter_copy_from(iter, rb->data + tail(rb), chunk, &copied);
|
||||
rb->len += copied;
|
||||
done += copied;
|
||||
if (copied < chunk)
|
||||
break;
|
||||
}
|
||||
*out_written = done;
|
||||
return done ? 0 : status;
|
||||
}
|
||||
|
||||
errno_t ringbuf_read_iter(struct ringbuf* rb, iovec_iter_t* iter, size_t len, size_t* out_read) {
|
||||
size_t done = 0;
|
||||
errno_t status = 0;
|
||||
size_t chunk;
|
||||
while (!status && (chunk = read_chunk(rb, len - done))) {
|
||||
size_t copied;
|
||||
status = iovec_iter_copy_to(iter, rb->data + rb->head, chunk, &copied);
|
||||
advance_read(rb, copied);
|
||||
done += copied;
|
||||
if (copied < chunk)
|
||||
break;
|
||||
}
|
||||
*out_read = done;
|
||||
return done ? 0 : status;
|
||||
}
|
||||
Reference in New Issue
Block a user