6.175: constructive computer architecture tutorial 6...

23
6.175: Constructive Computer Architecture Tutorial 6 Caches and Exceptions Quan Nguyen (Organizes his bookshelves like L1 and L2 caches) Nov 4, 2016 T06-1 http://csg.csail.mit.edu/6.175

Upload: others

Post on 24-Jul-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

6.175: Constructive Computer Architecture Tutorial 6

Caches and Exceptions Quan Nguyen (Organizes his bookshelves like L1 and L2 caches)

Nov 4, 2016 T06-1 http://csg.csail.mit.edu/6.175

Page 2: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Agenda •  Blocking caches •  Store buffer •  Lab 7 infrastructure •  Exception handling

Nov 4, 2016 T06-2 http://csg.csail.mit.edu/6.175

Page 3: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Blocking Cache Code

Nov 4, 2016 T06-3 http://csg.csail.mit.edu/6.175

Page 4: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Blocking Cache Interface

interface Cache; method Action req(MemReq r); method ActionValue#(Data) resp; method ActionValue#(LineReq) memReq; method Action memResp(Line r);

endinterface

cache

req

resp

memReq

memResp

Processor DRAM or next level cache hitQ

mReqQ

mRespQ

mshr

status

http://csg.csail.mit.edu/6.175 T06-4 Nov 4, 2016

Miss Status Handling Register

Page 5: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Blocking cache state elements module mkCache(Cache);

RegFile#(CacheIndex, Line) dataArray <- mkRegFileFull;

RegFile#(CacheIndex, Maybe#(CacheTag))

tagArray <- mkRegFileFull;

RegFile#(CacheIndex, Bool) dirtyArray <- mkRegFileFull;

Fifo#(1, Data) hitQ <- mkBypassFifo;

Reg#(MemReq) missReq <- mkRegU;

Reg#(CacheStatus) msrh <- mkReg(Ready);

Fifo#(2, LineReq) memReqQ <- mkCFFifo;

Fifo#(2, Line) memRespQ <- mkCFFifo;

function CacheIndex getIdx(Addr addr) = truncate(addr>>4);

function CacheTag getTag(Addr addr) = truncateLSB(addr); http://csg.csail.mit.edu/6.175 T06-5 Nov 4, 2016

Page 6: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Req method hit processing method Action req(MemReq r) if(mshr == Ready);

let idx = getIdx(r.addr); let tag = getTag(r.addr);

Bit#(2) wOffset = truncate(r.addr >> 2);

let currTag = tagArray.sub(idx);

let hit = isValid(currTag) ? fromMaybe(?, currTag) == tag

: False;

if (hit) begin

let x = dataArray.sub(idx);

if (r.op == Ld) hitQ.enq(x[wOffset]);

else begin

x[wOffset] = r.data;

dataArray.upd(idx, x); dirtyArray.upd(idx, True);

end

end else begin missReq <= r; mshr <= StartMiss; end

endmethod

http://csg.csail.mit.edu/6.175 T06-6 Nov 4, 2016

Page 7: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Start-miss rule

rule startMiss(mshr == StartMiss);

let idx = getIdx(missReq.addr);

let tag = tagArray.sub(idx);

let dirty = dirtyArray.sub(idx);

if (isValid(tag) && dirty) begin // write-back

let addr = {fromMaybe(?, tag), idx, 4'b0};

let data = dataArray.sub(idx);

memReqQ.enq(LineReq{op: St, addr: addr, data: data});

end

mshr <= SendFillReq;

endrule

Ready -> StartMiss -> SendFillReq -> WaitFillResp -> Ready

http://csg.csail.mit.edu/6.175 T06-7 Nov 4, 2016

Page 8: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Send-fill rule

Nov 4, 2016 T06-8 http://csg.csail.mit.edu/6.175

rule sendFillReq (mshr == SendFillReq);

memReqQ.enq(LineReq{op:Ld, addr:missReq.addr, data:?});

mshr <= WaitFillResp;

endrule

Ready -> StartMiss -> SendFillReq -> WaitFillResp -> Ready

Page 9: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Wait-fill rule Ready -> StartMiss -> SendFillReq -> WaitFillResp -> Ready

rule waitFillResp (mshr == WaitFillResp);

let idx = getIdx(missReq.addr);

let tag = getTag(missReq.addr);

let data = memRespQ.first;

tagArray.upd(idx, Valid (tag));

if (missReq.op == Ld) begin

dirtyArray.upd(idx, False); dataArray.upd(idx, data);

hitQ.enq(data[wOffset]);

end else begin

data[wOffset] = missReq.data;

dirtyArray.upd(idx, True); dataArray.upd(idx, data);

end

memRespQ.deq; mshr <= Ready;

endrule

http://csg.csail.mit.edu/6.175 T06-9 Nov 4, 2016

Page 10: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Rest of the methods method ActionValue#(Data) resp;

hitQ.deq;

return hitQ.first;

endmethod

method ActionValue#(MemReq) memReq;

memReqQ.deq;

return memReqQ.first;

endmethod

method Action memResp(Line r);

memRespQ.enq(r);

endmethod

Memory side methods

http://csg.csail.mit.edu/6.175 T06-10 Nov 4, 2016

Page 11: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Store Buffer Code

Nov 4, 2016 T06-11 http://csg.csail.mit.edu/6.175

Page 12: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Store Buffer: Req method hit processing method Action req(MemReq r) if (mshr == Ready);

// ... get idx, tag and wOffset

if (r.op == Ld) begin // search stb

let x = stb.search(r.addr); lockL1[0] <= True;

if (isValid(x)) hitQ.enq(fromMaybe(?, x));

else begin // search L1

let currTag = tagArray.sub(idx);

let hit = isValid(currTag) ? fromMaybe(?, currTag) == tag

: False;

if (hit) begin

let x = dataArray.sub(idx); hitQ.enq(x[wOffset]);

end else begin missReq <= r; mshr <= StartMiss; end

end

end else stb.enq(r.addr, r.data) // r.op == St

endmethod Nov 4, 2016 http://csg.csail.mit.edu/6.175 T06-12

Page 13: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Store Buffer to mReqQ

Nov 4, 2016 T06-13 http://csg.csail.mit.edu/6.175

rule mvStbToL1 (mshr == Ready && !lockL1[1]);

stb.deq; match {.addr, .data} = stb.first;

// ... get idx, tag and wOffset

let currTag = tagArray.sub(idx);

let hit = isValid(currTag) ? fromMaybe(?, currTag) == tag

: False;

if (hit) begin

let x = dataArray.sub(idx);

x[wOffset] = data; dataArray.upd(idx, x);

end else begin

missReq <= r; mshr <= StartMiss;

end

endrule

rule clearL1Lock; lockL1[1] <= False; endrule

Page 14: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Lab 7 FPGA Infrastructure

Nov 4, 2016 T06-14 http://csg.csail.mit.edu/6.175

Page 15: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

FPGA Infrastructure

•  SW-mkProc Interface n  Start PC n  mtohost n  Init DRAM

•  Avoid re-programming FPGA n  Reset FPGA after each test

Nov 4, 2016 T06-15 http://csg.csail.mit.edu/6.175

mkProc

1GB DRAM

SW testbench

Start PC

mtohost

Init DRAM

Page 16: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Simulation •  DRAM

n  RegFile + pipelined delay of resp

•  We also simulate DRAM initialization n  Longer simulation time

Nov 4, 2016 T06-16 http://csg.csail.mit.edu/6.175

Reg file ...

Page 17: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Cross Clock Domain Issues •  mkProc runs at 50MHz •  DRAM controller runs at 200MHz •  Cross clock domain

n  Non-atomic behavior at reset n  Deq DRAM resp FIFO to empty before start

new test n  Guard all rules with processor started

•  Lab 7: src/DDR3Example.bsv

Nov 4, 2016 T06-17 http://csg.csail.mit.edu/6.175

Page 18: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Exception Code

Nov 4, 2016 T06-18 http://csg.csail.mit.edu/6.175

Page 19: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

CSR •  mepc holds pc of the instruction that causes the interrupt •  mcause indicates the cause of the interrupt •  mscratch holds the pointer to HW-thread local storage for

saving context before handling the interrupt •  mstatus (privileged spec 1.7; outdated)

•  Four modes: Machine, Hypervisor, Supervisor, User §  We’ll only use Machine and User

Nov 4, 2016 T06-19 http://csg.csail.mit.edu/6.175

interrupt enabled?

Privilege mode

31 … 12 2 1 0

prv0 ie0 prv1 ie1 prv2 ie2 prv3 ie3 Other status bits

Page 20: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Interrupt handler- SW handler_entry: # fixed entry point for each mode # for user mode the address is 0x100 j interrupt_handler # One level of indirection interrupt_handler: # Common wrapper for all IH # get the pointer to HW-thread local stack csrrw sp, mscratch, sp # swap sp and mscratch # save x1, x3 ~ x31 to stack (x2 is sp, save later) addi sp, sp, -128 sw x1, 4(sp) sw x3, 12(sp) ... sw x31, 124(sp) # save original sp (now in mscratch) to stack csrr s0, mscratch # store mscratch to s0 sw s0, 8(sp)

Nov 4, 2016 http://csg.csail.mit.edu/6.175 T06-20

Page 21: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

Interrupt handler- SW cont. interrupt_handler: ... # we have saved all GPRs to stack # call C function to handle interrupt csrr a0, mcause # arg 0: cause csrr a1, mepc # arg 1: epc mv a2, sp # arg 2: sp – pointer to all saved GPRs jal c_handler # call C function # return value is the PC to resume csrw mepc, a0 # restore mscratch and all GPRs addi s0, sp, 128; csrw mscratch, s0 lw x1, 4(sp); lw x3, 12(sp); ...; lw x31, 124(sp) lw x2, 8(sp) # restore sp at last eret # finish handling interrupt

Nov 4, 2016 http://csg.csail.mit.edu/6.175 T06-21

•  Want to see a real interrupt handler? https://github.com/riscv/riscv-linux/blob/master/arch/riscv/kernel/entry.S

Page 22: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

C function to handle interrupt System call long c_handler(long cause, long epc, long *regs) { // regs[i] refers to GPR xi stored in stack if (cause == 0x08) { // cause code for SCALL is 8 // figure out the type of SCALL (stored in a7/x17) // args are in a0/x10, a1/x11, a2/x12 long type = regs[17]; long arg0 = regs[10]; long arg1 = regs[11]; long arg2 = regs[12]; if (type == SysPrintChar) { ... } else if (type == SysPrintInt) { ... } else if (type == SysExit) { ... } else ... // SCALL finshes, we need to resume to epc + 4 return epc + 4; } else if ...

Nov 4, 2016 http://csg.csail.mit.edu/6.175 T06-22

Page 23: 6.175: Constructive Computer Architecture Tutorial 6 ...csg.csail.mit.edu/6.175/lectures/T06-Caches-Exceptions.pdf · T06-Caches-Exceptions.pptx Author: Quan Nguyen Created Date:

C function to handle interrupt Emulated instruction – MUL

Nov 4, 2016 T06-23 http://csg.csail.mit.edu/6.175

long c_handler(long cause, long epc, long *regs) { if (cause == 0x08) { ... /* handle SCALL */ } else if (cause == 0x02) { // cause code for Illegal Instruction is 2 uint32_t inst = *((uint32_t *)epc); // fetch inst // check opcode & function codes if ((inst & MASK_MUL) == MATCH_MUL) { // is MUL, extract rd, rs1, rs2 fields int rd = (inst >> 7) & 0x01F; int rs1 = ...; int rs2 = ...; // emulate regs[rd] = regs[rs1] * regs[rs2] emulate_multiply(rd, rs1, rs2, regs); return epc + 4; // done, resume at epc+4 } else ... // try to match other inst } else ... // other causes }