Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions overlay/include/linux/kemu.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ void kemu_exit_to_user(struct pt_regs *regs);

/* Signal delivery and syscall restart for kemu tasks. */
void kemu_do_signal(struct pt_regs *regs);
/* syscall_rollback() on a kemu task (current): guest x0 gets orig_x0 back. */
void kemu_syscall_rollback(struct task_struct *task);

/* mm / task lifecycle. */
int kemu_dup_mm(struct mm_struct *oldmm, struct mm_struct *mm);
Expand All @@ -60,6 +62,13 @@ long kemu_arch_ptrace(struct task_struct *child, long request,
unsigned long addr, unsigned long data);
void kemu_user_step(struct task_struct *child, bool on);

/* ipc/sem.c: semctl on a kernel struct (kemu marshals the user layout,
* whose x86_64 and arm64 forms differ). Called from kemu and defined in
* ipc/sem.c, which reuses the static semctl_stat/semctl_down helpers. */
struct semid64_ds;
int kemu_semctl_stat(int semid, int cmd, struct semid64_ds *out);
int kemu_semctl_set(int semid, int cmd, struct semid64_ds *in);

/* mm / proc */
void kemu_unmapped(struct mm_struct *mm, unsigned long start, unsigned long end);
bool kemu_vma_hidden(struct vm_area_struct *vma);
Expand Down
91 changes: 91 additions & 0 deletions overlay/kernel/kemu/abi.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#include <linux/highuid.h>
#include <linux/kdev_t.h>
#include <linux/openat2.h>
#include <linux/audit.h>
#include <linux/sem.h>
#include <linux/ipc.h>

#include "kemu_internal.h"

Expand All @@ -28,6 +31,7 @@ asmlinkage long __x64_sys_open_by_handle_at(const struct pt_regs *regs);
asmlinkage long __x64_sys_fanotify_init(const struct pt_regs *regs);
asmlinkage long __x64_sys_newuname(const struct pt_regs *regs);
asmlinkage long __x64_sys_clone(const struct pt_regs *regs);
asmlinkage long __x64_sys_semctl(const struct pt_regs *regs);
asmlinkage long __x64_sys_epoll_pwait(const struct pt_regs *regs);
asmlinkage long __x64_sys_epoll_pwait2(const struct pt_regs *regs);
int do_sys_openat2(int dfd, const char __user *filename, struct open_how *how);
Expand Down Expand Up @@ -101,8 +105,13 @@ long kemu_sys_openat2(struct pt_regs *regs)
err = copy_struct_from_user(&how, sizeof(how), uhow, usize);
if (err)
return err;
/* Audit logs the guest's (arm64) flags, under AUDIT_ARCH_AARCH64. */
audit_openat2_how(&how);
/* Bits outside the translated set are validated by the core. */
how.flags = (how.flags & ~(u64)U32_MAX) | kemu_oflags_to_host((u32)how.flags);
/* O_LARGEFILE is forced on 64-bit (non-O_PATH), as in native openat2. */
if (!(how.flags & O_PATH) && force_o_largefile())
how.flags |= O_LARGEFILE;
return do_sys_openat2((int)kemu_reg(regs, di), (const char __user *)kemu_reg(regs, si), &how);
}

Expand Down Expand Up @@ -295,3 +304,85 @@ long kemu_sys_clone(struct pt_regs *regs)
swap(kemu_reg(regs, r10), kemu_reg(regs, r8));
return __x64_sys_clone(regs);
}

/* ---- semctl ----
*
* The x86_64 semid64_ds inserts two padding words (__unused1/__unused2)
* that asm-generic (arm64) doesn't, so its layout is 104 bytes where the
* guest's is 88. Only IPC_STAT/SEM_STAT/SEM_STAT_ANY (out) and IPC_SET (in)
* carry the struct; every other command is layout-agnostic and passes
* through. cmd never has the IPC_64 bit here (the entry fixes the version),
* matching the native switch; anything else falls through to the native
* handler, which rejects it exactly as arm64 would.
*/
struct kemu_ipc64_perm {
s32 key;
u32 uid, gid, cuid, cgid;
u32 mode;
u16 seq, __pad2;
u64 __unused1, __unused2;
};

struct kemu_semid64_ds {
struct kemu_ipc64_perm sem_perm;
s64 sem_otime;
s64 sem_ctime;
u64 sem_nsems;
u64 __unused3;
u64 __unused4;
};
static_assert(sizeof(struct kemu_semid64_ds) == 88);
static_assert(sizeof(struct kemu_ipc64_perm) == sizeof(struct ipc64_perm));

/* Copy the shared fields between the native and guest layouts. */
static void kemu_semid_to_guest(const struct semid64_ds *k, struct kemu_semid64_ds *g)
{
memset(g, 0, sizeof(*g));
memcpy(&g->sem_perm, &k->sem_perm, sizeof(g->sem_perm));
g->sem_otime = k->sem_otime;
g->sem_ctime = k->sem_ctime;
g->sem_nsems = k->sem_nsems;
}

static void kemu_semid_from_guest(const struct kemu_semid64_ds *g, struct semid64_ds *k)
{
memset(k, 0, sizeof(*k));
memcpy(&k->sem_perm, &g->sem_perm, sizeof(g->sem_perm));
k->sem_otime = g->sem_otime;
k->sem_ctime = g->sem_ctime;
k->sem_nsems = g->sem_nsems;
}

long kemu_sys_semctl(struct pt_regs *regs)
{
int semid = (int)kemu_reg(regs, di);
int cmd = (int)kemu_reg(regs, dx);
void __user *p = (void __user *)kemu_reg(regs, r10);
struct kemu_semid64_ds g;
struct semid64_ds k;
int err;

/* As ksys_semctl(): the stat/set helpers would mask a negative id
* into a valid index. */
if (semid < 0)
return -EINVAL;
switch (cmd) {
case IPC_STAT:
case SEM_STAT:
case SEM_STAT_ANY:
err = kemu_semctl_stat(semid, cmd, &k);
if (err < 0)
return err;
kemu_semid_to_guest(&k, &g);
if (copy_to_user(p, &g, sizeof(g)))
return -EFAULT;
return err; /* SEM_STAT* returns the semid */
case IPC_SET:
if (copy_from_user(&g, p, sizeof(g)))
return -EFAULT;
kemu_semid_from_guest(&g, &k);
return kemu_semctl_set(semid, cmd, &k);
default:
return __x64_sys_semctl(regs);
}
}
3 changes: 2 additions & 1 deletion overlay/kernel/kemu/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,8 @@ int kemu_dup_mm(struct mm_struct *oldmm, struct mm_struct *mm)
km->vdso_base = okm->vdso_base;
err = -ENOMEM;
km->chunks[0] = vzalloc(KEMU_STUBS_SIZE);
km->table = vmalloc(KEMU_TABLE_SIZE);
/* Zeroed: the whole table is mapped, but only part of it is written. */
km->table = vzalloc(KEMU_TABLE_SIZE);
if (!km->chunks[0] || !km->table)
goto out_unlock;
memcpy(km->chunks[0], okm->chunks[0], KEMU_STUBS_SIZE);
Expand Down
51 changes: 38 additions & 13 deletions overlay/kernel/kemu/jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,19 @@ static void kemu_inval_free(struct kemu_mm *km);

static void kemu_chunk_free(struct kemu_mm *km, int c)
{
vfree(km->chunks[c]);
kvfree(km->blockmap[c]);
km->chunks[c] = NULL;
km->blockmap[c] = NULL;
void *mem = km->chunks[c];
unsigned long *bm = km->blockmap[c];

WRITE_ONCE(km->chunks[c], NULL);
WRITE_ONCE(km->blockmap[c], NULL);
km->chunk_used[c] = 0;
km->chunk_retired[c] = 0;
/*
* kemu_materialize() reads chunk_retired locklessly and then trusts
* the pointers of a live (0) chunk: clear them first.
*/
smp_store_release(&km->chunk_retired[c], 0);
vfree(mem);
kvfree(bm);
}

void kemu_jit_free(struct kemu_mm *km)
Expand Down Expand Up @@ -468,7 +475,11 @@ static int kemu_chunk_new(struct kemu_mm *km)
kemu_jit_flush(km);
return -EAGAIN;
}
mem = __vmalloc(KEMU_CHUNK_SIZE, GFP_KERNEL_ACCOUNT);
/*
* Zeroed: kemu_code_fault() maps every page of a live chunk, including
* the unused tail and the padding between blocks.
*/
mem = __vmalloc(KEMU_CHUNK_SIZE, GFP_KERNEL_ACCOUNT | __GFP_ZERO);
bm = kvzalloc(BITS_TO_LONGS(KEMU_BLOCKMAP_BITS) * sizeof(long),
GFP_KERNEL_ACCOUNT);
if (!mem || !bm) {
Expand All @@ -480,7 +491,8 @@ static int kemu_chunk_new(struct kemu_mm *km)
km->blockmap[c] = bm;
km->chunk_used[c] = 0;
km->chunk_retired[c] = 0;
WRITE_ONCE(km->chunks[c], mem);
/* Publish the blockmap with the chunk (kemu_materialize is lockless). */
smp_store_release(&km->chunks[c], mem);
up_write(&km->chunk_sem);
km->cur_chunk = c;
return 0;
Expand All @@ -499,14 +511,17 @@ static u8 *kemu_find_block(struct kemu_mm *km, unsigned long off, size_t *len)
u64 gpc;
u32 main;
size_t sz;
u8 *blk;
u8 *chunk, *blk;

if (c == 0 || c >= KEMU_MAX_CHUNKS || !km->chunks[c] || in >= km->chunk_used[c])
if (c == 0 || c >= KEMU_MAX_CHUNKS)
return NULL;
chunk = smp_load_acquire(&km->chunks[c]);
if (!chunk || in >= km->chunk_used[c])
return NULL;
bit = find_last_bit(km->blockmap[c], in / KEMU_BLOCK_ALIGN + 1);
if (bit > in / KEMU_BLOCK_ALIGN)
return NULL;
blk = km->chunks[c] + bit * KEMU_BLOCK_ALIGN;
blk = chunk + bit * KEMU_BLOCK_ALIGN;
sz = kemu_rs_block_info(blk, km->chunk_used[c] - bit * KEMU_BLOCK_ALIGN, &gpc, &main);
if (!sz || in >= bit * KEMU_BLOCK_ALIGN + sz)
return NULL;
Expand Down Expand Up @@ -733,13 +748,23 @@ void kemu_materialize(struct pt_regs *regs)
kemu_rs_recover_stub(kemu_stub_info(km, ip - km->code_base),
&h, &kt->shadow);
} else if (ip >= km->code_base && ip < km->code_base + KEMU_CODE_SIZE) {
u64 retired;

off = ip - km->code_base;
blk = kemu_find_block(km, off, &len);
c = off >> KEMU_CHUNK_SHIFT;
/*
* A chunk retired in an epoch this thread has already
* acknowledged is not pinned by it (kemu_min_epoch_seen), so
* another thread's kemu_reclaim() may free it under us. Correct
* code never runs there: acknowledging an epoch resets the host
* return stack. Only a forged return address gets here.
*/
retired = smp_load_acquire(&km->chunk_retired[c]);
blk = retired && retired <= kt->epoch_seen ? NULL : kemu_find_block(km, off, &len);
if (!blk || !kemu_rs_recover(blk, len,
((u8 *)km->chunks[off >> KEMU_CHUNK_SHIFT] +
(off & (KEMU_CHUNK_SIZE - 1))) - blk,
&h, &kt->shadow)) {
c = off >> KEMU_CHUNK_SHIFT;
pr_warn_ratelimited("kemu: %s[%d]: cannot recover state at %lx (chunk %lu %s retired %llu used %x; epoch %llu seen %llu flags %lx)\n",
current->comm, task_pid_nr(current), ip, c,
km->chunks[c] ? "live" : "freed", km->chunk_retired[c],
Expand Down Expand Up @@ -996,7 +1021,7 @@ int kemu_jit_dup(struct kemu_mm *okm, struct kemu_mm *km)
for (c = 1; c < KEMU_MAX_CHUNKS; c++) {
if (!okm->chunks[c] || okm->chunk_retired[c])
continue;
km->chunks[c] = __vmalloc(KEMU_CHUNK_SIZE, GFP_KERNEL_ACCOUNT);
km->chunks[c] = __vmalloc(KEMU_CHUNK_SIZE, GFP_KERNEL_ACCOUNT | __GFP_ZERO);
km->blockmap[c] = kvmalloc(BITS_TO_LONGS(KEMU_BLOCKMAP_BITS) * sizeof(long),
GFP_KERNEL_ACCOUNT);
if (!km->chunks[c] || !km->blockmap[c])
Expand Down
2 changes: 2 additions & 0 deletions overlay/kernel/kemu/kemu_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ struct kemu_pgvec {
#define KEMU_TF_KSTATE BIT(2) /* state page authoritative (in kernel) */
#define KEMU_TF_STEP BIT(3) /* ptrace single-step */
#define KEMU_TF_STEP_SVC BIT(4) /* trap after the stepped svc */
#define KEMU_TF_ROLLBACK BIT(5) /* syscall_rollback() undid this svc */

struct kemu_thread {
struct task_struct *task;
Expand Down Expand Up @@ -251,6 +252,7 @@ long kemu_sys_epoll_pwait(struct pt_regs *regs);
long kemu_sys_epoll_pwait2(struct pt_regs *regs);
long kemu_sys_uname(struct pt_regs *regs);
long kemu_sys_clone(struct pt_regs *regs);
long kemu_sys_semctl(struct pt_regs *regs);

/* env.c: host services for the Rust core. */
int kemu_c_read(u64 addr, void *buf, size_t len);
Expand Down
Loading
Loading