Skip to content

Commit

Permalink
Make sure the tlb doesn't have an outdated mem pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed Oct 10, 2020
1 parent 8683e98 commit 3fee07f
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 7 deletions.
4 changes: 3 additions & 1 deletion emu/tlb.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "emu/cpu.h"
#include "emu/tlb.h"

void tlb_init(struct tlb *tlb, struct mem *mem) {
void tlb_refresh(struct tlb *tlb, struct mem *mem) {
if (tlb->mem == mem && tlb->mem_changes == mem->changes)
return;
tlb->mem = mem;
tlb->dirty_page = TLB_PAGE_EMPTY;
tlb->mem_changes = mem->changes;
Expand Down
2 changes: 1 addition & 1 deletion emu/tlb.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct tlb {
#define TLB_INDEX(addr) (((addr >> PAGE_BITS) & (TLB_SIZE - 1)) ^ (addr >> (PAGE_BITS + TLB_BITS)))
#define TLB_PAGE(addr) (addr & 0xfffff000)
#define TLB_PAGE_EMPTY 1
void tlb_init(struct tlb *tlb, struct mem *mem);
void tlb_refresh(struct tlb *tlb, struct mem *mem);
void tlb_free(struct tlb *tlb);
void tlb_flush(struct tlb *tlb);
void *tlb_handle_miss(struct tlb *tlb, addr_t addr, int type);
Expand Down
4 changes: 2 additions & 2 deletions jit/jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ static int cpu_single_step(struct cpu_state *cpu, struct tlb *tlb) {

int cpu_run_to_interrupt(struct cpu_state *cpu, struct tlb *tlb) {
read_wrlock(&cpu->mem->lock);
if (cpu->mem->changes != tlb->mem_changes)
tlb_init(tlb, cpu->mem);
printk("%d cpu run with mem %p %d, tlb->mem %p %d\n", current->pid, cpu->mem, cpu->mem->changes, tlb->mem, tlb->mem_changes);
tlb_refresh(tlb, cpu->mem);
int interrupt = (cpu->tf ? cpu_single_step : cpu_step_to_interrupt)(cpu, tlb);
cpu->trapno = interrupt;
read_wrunlock(&cpu->mem->lock);
Expand Down
2 changes: 2 additions & 0 deletions kernel/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,10 @@ static int elf_exec(struct fd *fd, const char *file, struct exec_args argv, stru
// from this point on, if any error occurs the process will have to be
// killed before it even starts. please don't be too sad about it, it's
// just a process.
struct mm *old_mm = current->mm;
mm_release(current->mm);
task_set_mm(current, mm_new());
printk("%d replacing %p with %p\n", current->pid, &old_mm->mem, &current->mm->mem);
write_wrlock(&current->mem->lock);

current->mm->exefile = fd_retain(fd);
Expand Down
2 changes: 1 addition & 1 deletion kernel/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void task_destroy(struct task *task) {
void task_run_current() {
struct cpu_state *cpu = &current->cpu;
struct tlb tlb;
tlb_init(&tlb, current->mem);
tlb_refresh(&tlb, current->mem);
while (true) {
int interrupt = cpu_run_to_interrupt(cpu, &tlb);
handle_interrupt(interrupt);
Expand Down
2 changes: 1 addition & 1 deletion tools/ptraceomatic.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ int main(int argc, char *const argv[]) {
struct cpu_state *cpu = &current->cpu;
cpu->tf = true;
struct tlb tlb;
tlb_init(&tlb, cpu->mem);
tlb_refresh(&tlb, cpu->mem);
int undefined_flags = 2;
struct cpu_state old_cpu = *cpu;
int i = 0;
Expand Down
2 changes: 1 addition & 1 deletion tools/unicornomatic.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ int main(int argc, char *const argv[]) {

struct cpu_state *cpu = &current->cpu;
struct tlb tlb;
tlb_init(&tlb, cpu->mem);
tlb_refresh(&tlb, cpu->mem);
int undefined_flags = 0;
struct cpu_state old_cpu = *cpu;
while (true) {
Expand Down

0 comments on commit 3fee07f

Please sign in to comment.