Skip to content

Commit

Permalink
Adds a new system call s3k_mon_yield
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrik Karlsson committed Oct 9, 2023
1 parent 22507c7 commit 0d01aae
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
3 changes: 3 additions & 0 deletions common/inc/s3k/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ typedef enum {
// Monitor calls
S3K_SYS_MON_SUSPEND,
S3K_SYS_MON_RESUME,
S3K_SYS_MON_YIELD,
S3K_SYS_MON_REG_READ,
S3K_SYS_MON_REG_WRITE,
S3K_SYS_MON_CAP_READ,
Expand Down Expand Up @@ -109,6 +110,7 @@ s3k_err_t s3k_pmp_load(s3k_cidx_t pmp_idx, s3k_pmp_slot_t pmp_slot);
s3k_err_t s3k_pmp_unload(s3k_cidx_t pmp_idx);
s3k_err_t s3k_mon_suspend(s3k_cidx_t mon_idx, s3k_pid_t pid);
s3k_err_t s3k_mon_resume(s3k_cidx_t mon_idx, s3k_pid_t pid);
s3k_err_t s3k_mon_yield(s3k_cidx_t mon_idx, s3k_pid_t pid);
s3k_err_t s3k_mon_reg_read(s3k_cidx_t mon_idx, s3k_pid_t pid, s3k_reg_t reg,
uint64_t *val);
s3k_err_t s3k_mon_reg_write(s3k_cidx_t mon_idx, s3k_pid_t pid, s3k_reg_t reg,
Expand All @@ -133,6 +135,7 @@ s3k_err_t s3k_try_pmp_load(s3k_cidx_t pmp_idx, s3k_pmp_slot_t pmp_slot);
s3k_err_t s3k_try_pmp_unload(s3k_cidx_t pmp_idx);
s3k_err_t s3k_try_mon_suspend(s3k_cidx_t mon_idx, s3k_pid_t pid);
s3k_err_t s3k_try_mon_resume(s3k_cidx_t mon_idx, s3k_pid_t pid);
s3k_err_t s3k_try_mon_yield(s3k_cidx_t mon_idx, s3k_pid_t pid);
s3k_err_t s3k_try_mon_reg_read(s3k_cidx_t mon_idx, s3k_pid_t pid, s3k_reg_t reg,
uint64_t *val);
s3k_err_t s3k_try_mon_reg_write(s3k_cidx_t mon_idx, s3k_pid_t pid,
Expand Down
17 changes: 17 additions & 0 deletions common/src/s3k/s3k.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,15 @@ s3k_err_t s3k_mon_resume(s3k_cidx_t mon_idx, s3k_pid_t pid)
return err;
}

s3k_err_t s3k_mon_yield(s3k_cidx_t mon_idx, s3k_pid_t pid)
{
s3k_err_t err;
do {
err = s3k_try_mon_yield(mon_idx, pid);
} while (err == S3K_ERR_PREEMPTED);
return err;
}

s3k_err_t s3k_mon_reg_read(s3k_cidx_t mon_idx, s3k_pid_t pid, s3k_reg_t reg,
uint64_t *val)
{
Expand Down Expand Up @@ -622,6 +631,14 @@ s3k_err_t s3k_try_mon_resume(s3k_cidx_t mon, s3k_pid_t pid)
return do_ecall(S3K_SYS_MON_RESUME, args).err;
}

s3k_err_t s3k_try_mon_yield(s3k_cidx_t mon, s3k_pid_t pid)
{
sys_args_t args = {
.mon_state = {mon, pid}
};
return do_ecall(S3K_SYS_MON_YIELD, args).err;
}

s3k_err_t s3k_try_mon_reg_read(s3k_cidx_t mon, s3k_pid_t pid, s3k_reg_t reg,
uint64_t *val)
{
Expand Down
12 changes: 12 additions & 0 deletions kernel/inc/cap_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ err_t cap_monitor_suspend(cte_t mon, pid_t pid);
*/
err_t cap_monitor_resume(cte_t mon, pid_t pid);

/**
* Yield remaining execution time to specified ready process.
*
* @param mon The CTE of the monitor capability.
* @param pid The ID of the process to be run.
* @return SUCCESS if the process is resumed.
* ERR_EMPTY if the CTE is empty.
* ERR_INVALID_MONITOR if the CTE has the wrong capability type or is
* unauthorized. ERR_INVALID_STATE if the process is not ready.
*/
err_t cap_monitor_yield(cte_t mon, pid_t pid, proc_t **next);

/**
* Reads a register value from a specified suspended process.
*
Expand Down
1 change: 1 addition & 0 deletions kernel/inc/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ typedef enum {
// Monitor
SYS_MON_SUSPEND,
SYS_MON_RESUME,
SYS_MON_YIELD,
SYS_MON_REG_READ,
SYS_MON_REG_WRITE,
SYS_MON_CAP_READ,
Expand Down
16 changes: 16 additions & 0 deletions kernel/src/cap_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ err_t cap_monitor_resume(cte_t mon, pid_t pid)
return err;
}

err_t cap_monitor_yield(cte_t mon, pid_t pid, proc_t **next)
{
err_t err = check_monitor(mon, pid, false);
if (!err) {
proc_t *proc = proc_get(pid);
proc_t *curr = proc_get(cte_pid(mon));
if (proc_acquire(proc)) {
curr->regs[REG_T0] = SUCCESS;
*next = proc;
return YIELD;
}
curr->regs[REG_T0] = ERR_INVALID_STATE;
}
return err;
}

err_t cap_monitor_reg_read(cte_t mon, pid_t pid, reg_t reg, uint64_t *val)
{
err_t err = check_monitor(mon, pid, true);
Expand Down
20 changes: 14 additions & 6 deletions kernel/src/syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static err_t sys_pmp_load(proc_t *p, const sys_args_t *args, uint64_t *ret);
static err_t sys_pmp_unload(proc_t *p, const sys_args_t *args, uint64_t *ret);
static err_t sys_mon_suspend(proc_t *p, const sys_args_t *args, uint64_t *ret);
static err_t sys_mon_resume(proc_t *p, const sys_args_t *args, uint64_t *ret);
static err_t sys_mon_yield(proc_t *p, const sys_args_t *args, uint64_t *ret);
static err_t sys_mon_reg_read(proc_t *p, const sys_args_t *args, uint64_t *ret);
static err_t sys_mon_reg_write(proc_t *p, const sys_args_t *args,
uint64_t *ret);
Expand All @@ -50,12 +51,12 @@ static err_t sys_sock_sendrecv(proc_t *p, const sys_args_t *args,
typedef err_t (*sys_handler_t)(proc_t *, const sys_args_t *, uint64_t *);

sys_handler_t handlers[] = {
sys_get_info, sys_reg_read, sys_reg_write, sys_sync,
sys_cap_read, sys_cap_move, sys_cap_delete, sys_cap_revoke,
sys_cap_derive, sys_pmp_load, sys_pmp_unload, sys_mon_suspend,
sys_mon_resume, sys_mon_reg_read, sys_mon_reg_write, sys_mon_cap_read,
sys_mon_cap_move, sys_mon_pmp_load, sys_mon_pmp_unload, sys_sock_send,
sys_sock_sendrecv,
sys_get_info, sys_reg_read, sys_reg_write, sys_sync,
sys_cap_read, sys_cap_move, sys_cap_delete, sys_cap_revoke,
sys_cap_derive, sys_pmp_load, sys_pmp_unload, sys_mon_suspend,
sys_mon_resume, sys_mon_yield, sys_mon_reg_read, sys_mon_reg_write,
sys_mon_cap_read, sys_mon_cap_move, sys_mon_pmp_load, sys_mon_pmp_unload,
sys_sock_send, sys_sock_sendrecv,
};

void handle_syscall(proc_t *p)
Expand Down Expand Up @@ -201,6 +202,7 @@ err_t validate_arguments(uint64_t call, const sys_args_t *args)

case SYS_MON_SUSPEND:
case SYS_MON_RESUME:
case SYS_MON_YIELD:
if (!valid_idx(args->mon_state.mon_idx))
return ERR_INVALID_INDEX;
if (!valid_pid(args->mon_state.pid))
Expand Down Expand Up @@ -390,6 +392,12 @@ err_t sys_mon_resume(proc_t *p, const sys_args_t *args, uint64_t *ret)
return cap_monitor_resume(mon, args->mon_state.pid);
}

err_t sys_mon_yield(proc_t *p, const sys_args_t *args, uint64_t *ret)
{
cte_t mon = ctable_get(p->pid, args->mon_state.mon_idx);
return cap_monitor_yield(mon, args->mon_state.pid, (proc_t **)ret);
}

err_t sys_mon_reg_read(proc_t *p, const sys_args_t *args, uint64_t *ret)
{
cte_t mon = ctable_get(p->pid, args->mon_reg.mon_idx);
Expand Down

0 comments on commit 0d01aae

Please sign in to comment.