Skip to content

Commit

Permalink
x86_64: performance fixes when switching I/O port permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Sep 30, 2024
1 parent a70f33a commit a5603f4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/platform/x86_64/include/platform/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ typedef struct {

ThreadGPR regs; // register state

int iopl; // set to 1 if I/O port privileges have been modified
uint8_t ioports[8192]; // I/O port privileges
} __attribute__((packed)) ThreadContext;

Expand Down
1 change: 1 addition & 0 deletions src/platform/x86_64/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ int platformIoperm(Thread *t, uintptr_t from, uintptr_t count, int enable) {
if((from+count-1) > 0xFFFF) return -EINVAL; // 65536 I/O ports on x86

ThreadContext *ctx = (ThreadContext *) t->context;
ctx->iopl = 1;

for(int i = 0; i < count; i++) {
int byte = (from + i) / 8;
Expand Down
10 changes: 8 additions & 2 deletions src/platform/x86_64/sched/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,14 @@ void platformSwitchContext(Thread *t) {
ThreadContext *ctx = (ThreadContext *)t->context;
ctx->regs.rflags |= 0x202; // interrupts can never be switched off outside of the kernel

// modify the TSS with the current thread's I/O permissions
memcpy(kinfo->tss->ioports, ctx->ioports, 8192);
// modify the TSS with the current thread's I/O permissions if necessary
Thread *old = kinfo->thread;
ThreadContext *oldctx = NULL;
if(old) oldctx = (ThreadContext *) old->context;

if(ctx->iopl || (oldctx && oldctx->iopl)) {
memcpy(kinfo->tss->ioports, ctx->ioports, 8192);
}

kinfo->thread = t;
kinfo->process = getProcess(t->pid);
Expand Down

0 comments on commit a5603f4

Please sign in to comment.