Skip to content

Commit

Permalink
Refactors API
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrik Karlsson committed Mar 30, 2023
1 parent d169007 commit 2ccea83
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 252 deletions.
5 changes: 4 additions & 1 deletion api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ CFLAGS=-march=rv64imac -mabi=lp64 -mcmodel=medany
CFLAGS+=-ffat-lto-objects -flto
CFLAGS+=-Os -g

clean: s3k-syscall.o s3k-utils.o
all: libs3k.a

libs3k.a: s3k-syscall.o s3k-utils.o
$(AR) rcs -o $@ $^

clean:
rm -f s3k-syscall.o s3k-utils.o

.PHONY: clean
273 changes: 86 additions & 187 deletions api/s3k-utils.c
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
#include "s3k.h"

const char *s3k_error2str(enum s3k_excpt code)
{
switch (code) {
case S3K_EXCPT_NONE:
return "S3K_EXCPT_NONE";
case S3K_EXCPT_EMPTY:
return "S3K_EXCPT_EMPTY";
case S3K_EXCPT_COLLISION:
return "S3K_EXCPT_COLLISION";
case S3K_EXCPT_DERIVATION:
return "S3K_EXCPT_DERIVATION";
case S3K_EXCPT_PREEMPTED:
return "S3K_EXCPT_PREEMPETED";
case S3K_EXCPT_SUSPENDED:
return "S3K_EXCPT_SUSPENDED";
case S3K_EXCPT_MPID:
return "S3K_EXCPT_MPID";
case S3K_EXCPT_MBUSY:
return "S3K_EXCPT_MBUSY";
case S3K_EXCPT_UNIMPLEMENTED:
return "S3K_EXCPT_UNIMPLEMENTED";
case S3K_EXCPT_INVALID_CAP:
return "S3K_EXCPT_INVALID_CAP";
case S3K_EXCPT_NO_RECEIVER:
return "S3K_EXCPT_NO_RECEIVER";
case S3K_EXCPT_SEND_CAP:
return "S3K_EXCPT_SEND_CAP";
default:
return "UNKNOWN";
}
}

uint64_t s3k_pmp_napot_addr(uint64_t begin, uint64_t end)
{
return (begin | (((end - begin) - 1) >> 1)) >> 2;
Expand Down Expand Up @@ -91,101 +59,6 @@ union s3k_cap s3k_socket(uint64_t channel, uint64_t tag)
};
}

bool s3k_time_derive_time(struct s3k_time parent, struct s3k_time child)
{
return parent.free == child.begin && child.end <= parent.end
&& child.begin == child.free && child.begin < child.end
&& child.hartid == parent.hartid && child._padd == 0;
}

bool s3k_memory_derive_memory(struct s3k_memory parent, struct s3k_memory child)
{
return parent.free == child.begin && child.end <= parent.end
&& child.begin == child.free && child.begin < child.end
&& (parent.rwx & child.rwx) == child.rwx && parent.lock == 0;
}

static bool s3k_memory_derive_pmp(struct s3k_memory parent,
struct s3k_pmp child)
{
uint64_t pmp_begin = s3k_pmp_napot_begin(child.addr);
uint64_t pmp_end = s3k_pmp_napot_end(child.addr);
uint64_t rwx = child.cfg & 0x7;
uint64_t mode = child.cfg >> 3;
uint64_t mem_free
= ((uint64_t)parent.offset << 27) + (parent.free << 12);
uint64_t mem_end = ((uint64_t)parent.offset << 27) + (parent.end << 12);
return mem_free <= pmp_begin && pmp_end <= mem_end
&& ((parent.rwx & rwx) == rwx) && mode == 0x3;
}

bool s3k_monitor_derive_monitor(struct s3k_monitor parent,
struct s3k_monitor child)
{
return parent.free == child.begin && child.end <= parent.end
&& child.begin == child.free && child.begin < child.end;
}

bool s3k_channel_derive_channel(struct s3k_channel parent,
struct s3k_channel child)
{
return parent.free == child.begin && child.end <= parent.end
&& child.begin == child.free && child.begin < child.end
&& child._padd == 0;
}

bool s3k_channel_derive_socket(struct s3k_channel parent,
struct s3k_socket child)
{
return parent.free == child.channel && child.channel < parent.end
&& child.tag == 0 && child._padd == 0;
}

bool s3k_socket_derive_socket(struct s3k_socket parent, struct s3k_socket child)
{
return parent.channel == child.channel && parent.tag == 0
&& child.tag > 0 && child._padd == 0;
}

bool s3k_time_derive(union s3k_cap parent, union s3k_cap child)
{
return parent.type == S3K_CAPTY_TIME && child.type == S3K_CAPTY_TIME
&& s3k_time_derive_time(parent.time, child.time);
}

bool s3k_memory_derive(union s3k_cap parent, union s3k_cap child)
{
return (parent.type == S3K_CAPTY_MEMORY
&& child.type == S3K_CAPTY_MEMORY
&& s3k_memory_derive_memory(parent.memory, child.memory))
|| (parent.type == S3K_CAPTY_MEMORY
&& child.type == S3K_CAPTY_PMP
&& s3k_memory_derive_pmp(parent.memory, child.pmp));
}

bool s3k_monitor_derive(union s3k_cap parent, union s3k_cap child)
{
return parent.type == S3K_CAPTY_MONITOR
&& child.type == S3K_CAPTY_MONITOR
&& s3k_monitor_derive_monitor(parent.monitor, child.monitor);
}

bool s3k_channel_derive(union s3k_cap parent, union s3k_cap child)
{
return (parent.type == S3K_CAPTY_CHANNEL
&& child.type == S3K_CAPTY_CHANNEL
&& s3k_channel_derive_channel(parent.channel, child.channel))
|| (parent.type == S3K_CAPTY_CHANNEL
&& child.type == S3K_CAPTY_SOCKET
&& s3k_channel_derive_socket(parent.channel, child.socket));
}

bool s3k_socket_derive(union s3k_cap parent, union s3k_cap child)
{
return parent.type == S3K_CAPTY_SOCKET && child.type == S3K_CAPTY_SOCKET
&& s3k_socket_derive_socket(parent.socket, child.socket);
}

bool s3k_can_derive(union s3k_cap parent, union s3k_cap child)
{
return s3k_time_derive(parent, child)
Expand All @@ -195,99 +68,125 @@ bool s3k_can_derive(union s3k_cap parent, union s3k_cap child)
|| s3k_socket_derive(parent, child);
}

bool s3k_time_parent_time(struct s3k_time parent, struct s3k_time child)
{
return parent.begin <= child.begin && child.end <= parent.end
&& child.hartid == parent.hartid;
}

static bool s3k_memory_parent_memory(struct s3k_memory parent,
struct s3k_memory child)
bool s3k_is_parent(union s3k_cap parent, union s3k_cap child)
{
return parent.offset == child.offset && parent.begin <= child.begin
&& child.end <= parent.end;
return s3k_time_parent(parent, child)
|| s3k_memory_parent(parent, child)
|| s3k_monitor_parent(parent, child)
|| s3k_channel_parent(parent, child)
|| s3k_socket_parent(parent, child);
}

static bool s3k_memory_parent_pmp(struct s3k_memory parent,
struct s3k_pmp child)
bool s3k_time_parent(union s3k_cap parent, union s3k_cap child)
{
uint64_t pmp_begin = s3k_pmp_napot_begin(child.addr);
uint64_t pmp_end = s3k_pmp_napot_end(child.addr);
uint64_t rwx = child.cfg & 0x7;
uint64_t mem_free
= ((uint64_t)parent.offset << 27) + (parent.free << 12);
uint64_t mem_end = ((uint64_t)parent.offset << 27) + (parent.end << 12);
return mem_free <= pmp_begin && pmp_end <= mem_end
&& ((parent.rwx & rwx) == rwx);
return parent.type == S3K_CAPTY_TIME && child.type == S3K_CAPTY_TIME
&& parent.time.begin <= child.time.begin
&& child.time.end <= parent.time.end
&& child.time.hartid == parent.time.hartid;
}

bool s3k_monitor_parent_monitor(struct s3k_monitor parent,
struct s3k_monitor child)
bool s3k_memory_parent(union s3k_cap parent, union s3k_cap child)
{
return parent.begin <= child.begin && child.end <= parent.end;
if (parent.type == S3K_CAPTY_MEMORY && child.type == S3K_CAPTY_MEMORY) {
return parent.memory.offset == child.memory.offset
&& parent.memory.begin <= child.memory.begin
&& child.memory.end <= parent.memory.end;
}
if (parent.type == S3K_CAPTY_MEMORY && child.type == S3K_CAPTY_PMP) {
uint64_t pmp_begin = s3k_pmp_napot_begin(child.pmp.addr);
uint64_t pmp_end = s3k_pmp_napot_end(child.pmp.addr);
uint64_t mem_begin = ((uint64_t)parent.memory.offset << 27)
+ (parent.memory.begin << 12);
uint64_t mem_end = ((uint64_t)parent.memory.offset << 27)
+ (parent.memory.end << 12);
return mem_begin <= pmp_begin && pmp_end <= mem_end;
}
return false;
}

bool s3k_channel_parent_channel(struct s3k_channel parent,
struct s3k_channel child)
bool s3k_monitor_parent(union s3k_cap parent, union s3k_cap child)
{
return parent.begin <= child.begin && child.end <= parent.end;
return parent.type == S3K_CAPTY_MONITOR
&& child.type == S3K_CAPTY_MONITOR
&& parent.monitor.begin <= child.monitor.begin
&& child.monitor.end <= parent.monitor.end;
}

bool s3k_channel_parent_socket(struct s3k_channel parent,
struct s3k_socket child)
bool s3k_channel_parent(union s3k_cap parent, union s3k_cap child)
{
return parent.begin <= child.channel && child.channel <= parent.end;
if (parent.type == S3K_CAPTY_CHANNEL && child.type == S3K_CAPTY_CHANNEL)
return parent.channel.begin <= child.channel.begin
&& child.channel.end <= parent.channel.end;
if (parent.type == S3K_CAPTY_CHANNEL && child.type == S3K_CAPTY_SOCKET)
return parent.channel.begin <= child.socket.channel
&& child.socket.channel < parent.channel.end;
return false;
}

bool s3k_socket_parent_socket(struct s3k_socket parent, struct s3k_socket child)
bool s3k_socket_parent(union s3k_cap parent, union s3k_cap child)
{
return parent.tag == 0 && parent.channel == child.channel;
return parent.type == S3K_CAPTY_SOCKET && child.type == S3K_CAPTY_SOCKET
&& parent.socket.tag == 0
&& parent.socket.channel == child.socket.channel;
}

bool s3k_time_parent(union s3k_cap parent, union s3k_cap child)
bool s3k_time_derive(union s3k_cap parent, union s3k_cap child)
{
return parent.type == S3K_CAPTY_TIME && child.type == S3K_CAPTY_TIME
&& s3k_time_parent_time(parent.time, child.time);
&& parent.time.free == child.time.begin
&& parent.time.free == child.time.free
&& child.time.end <= parent.time.end
&& child.time.hartid == parent.time.hartid;
}

bool s3k_memory_parent(union s3k_cap parent, union s3k_cap child)
bool s3k_memory_derive(union s3k_cap parent, union s3k_cap child)
{
return (parent.type == S3K_CAPTY_MEMORY
&& child.type == S3K_CAPTY_MEMORY
&& s3k_memory_parent_memory(parent.memory, child.memory))
|| (parent.type == S3K_CAPTY_MEMORY
&& child.type == S3K_CAPTY_PMP
&& s3k_memory_parent_pmp(parent.memory, child.pmp));
if (parent.type == S3K_CAPTY_MEMORY && child.type == S3K_CAPTY_MEMORY) {
return parent.memory.offset == child.memory.offset
&& parent.memory.free == child.memory.begin
&& parent.memory.free == child.memory.free
&& child.memory.end <= parent.memory.end;
}
if (parent.type == S3K_CAPTY_MEMORY && child.type == S3K_CAPTY_PMP) {
uint64_t pmp_begin = s3k_pmp_napot_begin(child.pmp.addr);
uint64_t pmp_end = s3k_pmp_napot_end(child.pmp.addr);
uint64_t pmp_rwx = child.pmp.cfg & 0x7;
uint64_t pmp_mode = child.pmp.cfg >> 3;
uint64_t mem_free = ((uint64_t)parent.memory.offset << 27)
+ (parent.memory.free << 12);
uint64_t mem_end = ((uint64_t)parent.memory.offset << 27)
+ (parent.memory.end << 12);
uint64_t mem_rwx = parent.memory.rwx;
return mem_free <= pmp_begin && pmp_end <= mem_end
&& pmp_mode == 0x3 && (mem_rwx & pmp_rwx) == pmp_rwx;
}
return false;
}

bool s3k_monitor_parent(union s3k_cap parent, union s3k_cap child)
bool s3k_monitor_derive(union s3k_cap parent, union s3k_cap child)
{
return parent.type == S3K_CAPTY_MONITOR
&& child.type == S3K_CAPTY_MONITOR
&& s3k_monitor_parent_monitor(parent.monitor, child.monitor);
&& parent.monitor.free == child.monitor.begin
&& parent.monitor.free == child.monitor.free
&& child.monitor.end <= parent.monitor.end;
}

bool s3k_channel_parent(union s3k_cap parent, union s3k_cap child)
bool s3k_channel_derive(union s3k_cap parent, union s3k_cap child)
{
return (parent.type == S3K_CAPTY_CHANNEL
&& child.type == S3K_CAPTY_CHANNEL
&& s3k_channel_parent_channel(parent.channel, child.channel))
|| (parent.type == S3K_CAPTY_CHANNEL
&& child.type == S3K_CAPTY_SOCKET
&& s3k_channel_parent_socket(parent.channel, child.socket));
if (parent.type == S3K_CAPTY_CHANNEL && child.type == S3K_CAPTY_CHANNEL)
return parent.channel.free == child.channel.begin
&& parent.channel.free == child.channel.free
&& child.channel.end <= parent.channel.end;
if (parent.type == S3K_CAPTY_CHANNEL && child.type == S3K_CAPTY_SOCKET)
return parent.channel.free == child.socket.channel
&& child.socket.channel < parent.channel.end;
return false;
}

bool s3k_socket_parent(union s3k_cap parent, union s3k_cap child)
bool s3k_socket_derive(union s3k_cap parent, union s3k_cap child)
{
return parent.type == S3K_CAPTY_SOCKET && child.type == S3K_CAPTY_SOCKET
&& s3k_socket_parent_socket(parent.socket, child.socket);
}

bool s3k_is_parent(union s3k_cap parent, union s3k_cap child)
{
return s3k_time_parent(parent, child)
|| s3k_memory_parent(parent, child)
|| s3k_monitor_parent(parent, child)
|| s3k_channel_parent(parent, child)
|| s3k_socket_parent(parent, child);
&& parent.socket.tag == 0 && child.socket.tag > 0
&& parent.socket.channel == child.socket.channel;
}
Loading

0 comments on commit 2ccea83

Please sign in to comment.