Schema di definizione di n Sistema Operativo Schema generale Realizzazione della macchina hardware Varietà di architettre HW Gerarchie di macchine varietà di sistemi operativi!
Tradzione Tradzione Sorce int a, b, c, d;... a = b c; d = a - 100; Assembly Langage ; Code for a = b c load R3,b load R,c add R3,R store R3,a ; Code for d = a - 100 load R,=100 sbtract R3,R store R3,d Assembly Langage ; Code for a = b c load R3,b load R,c add R3,R store R3,a ; Code for d = a - 100 load R,=100 sbtract R3,R store R3,d achine Langage 10111001001100 1 10111001010000 0 10100111001100 0 10111010001100 1 10111001010000 0 10100110001100 0 10111001101100 1 Architettra di von Nemann Architettra di von Nemann: memoria Central Processing Unit (CPU) Data Path Control Unit (CU) Central Processing Unit (CPU) Data Path Control Unit (CU) Address Bs Data Bs Address Bs Data Bs Primary emory Unit (Eectable emory) Device Interface Primary emory Unit (Eectable emory) Device Interface
emoria centrale Architettra di von Nemann AR DR 123 98765 0 1 2 Central Processing Unit (CPU) Command read Data Path Control Unit (CU) Read Op: 1. Load AR con indirizzo 2. Load Command con read 3. Trasferisci dato in DR Contiene programmi dati. 123 98765 n-1 Address Bs Data Bs Primary emory Unit (Eectable emory) Device Interface Progetto del Datapath Progetto del Datapath Address Bs Data Bs Central Processing Unit (CPU) Data Path Primary emory Unit (Eectable emory) Control Unit (CU) Device Interface Eqivalenza acchina <-> Lingaggio Progettazione del set di istrzioni Filosofia RISC vs CISC Scelta del formato dell istrzione: dove stanno gli operandi? (registro/memoria) qanti operandi? Determinazione del set di istrzioni Scelta delle modalità di indirizzamento
Progetto del Datapath Un programma esegibile dalla macchina di Von Nemann consiste in na lista di istrzioni registrate in memoria centrale Possibili formati di n istrzione C.O. Op1 Op2 Op3 C.O. Op1 Op2 C.O. C.O. Op1 13 ADD R3 LOAD 100 JUP 20 HALT Progetto del Datapath Il set di istrzioni definisce il lingaggio della macchina Istrzioni di LOAD / STORE LOAD Rd Ind!!! Rd <- em[ind] STORE Rs Ind!! Rs -> em[ind] Istrzioni aritmetico/logiche ADD Rd Rs1 Rs2!! Rd <- Rs1 Rs2 SUB Rd Rs1 Rs2!! Rd <- Rs1 - Rs2 ULT Rd Rs1 Rs2!! Rd <- Rs1 Rs2 DIV!Rd Rs1 Rs2!! Rd <- Rs1 / Rs2 OR!Rd Rs1 Rs2!! Rd <- Rs1 or Rs2 AND! Rd Rs1 Rs2!! Rd <- Rs1 and Rs2 1 Progetto del Datapath Istrzioni di confronto, confronto/salto, salto SLT Rd Rs1 Rs2!! se Rs1 < Rs2 allora Rd<-1!!!!!! altrimenti Rd <- 0 BEQ Rs1 Rs2 Ind!! se Rs1 = Rs2 va!!!!! all istrzione di indirizzo Ind BNEQ Rs1 Rs2 Ind!! se Rs1! Rs2 va!!!!! all istrzione di indirizzo Ind JUP Ind!!! va all istrzione di indirizzo!!!! Ind Altre... HALT!!! Termina l eseczione del!!!! programma Progetto del Datapath Si possono tilizzare diverse modalità di indirizzamento per specificare n operando Indirizzamento a registro: ADD R3 R3 Indirizzamento immediato: ADD R3 #50 R3 100 20 R3 100 20 120 20 20 R3 70 15 16
Indirizzamento in memoria: Assolto: LOAD 2 R3 R3 1 2 3 em 10 22 cc em 22 1 2 3 10 22 cc Indirizzamento in memoria: Con spiazzamento: LOAD 1() R3 R3 2 1 1 2 3 em 10 22 cc em cc 1 2 3 10 22 cc 17 18 Progetto del Datapath Le istrzioni sono esegite na alla volta secondo l'ordine specificato nel programma Qando si incontra n istrzione di controllo, si altera il flsso seqenziale stabilendo il nmero d ordine della sccessiva istrzione da esegire. ESEPIO Come esempio consideriamo n programma che esege la somma di de nmeri letti in memoria e salva il risltato in memoria. 0 LOAD 100 1 LOAD 10 2 ADD R3 3 STORE R3 108 HALT 20
Progetto del Datapath ESEPIO: che cosa compta il segente programma? 0 LOAD R0 #100 1 LOAD 1(R0) 2 LOAD 2(R0) 3 BEQ #0 6 DIV R3 5 STORE R3 3(R0) 6 HALT R0 R3 21 100 101 102 103 em 10 0 Progetto del Datapath: registri! I registri generali:,,, Rn! il registro degli indirizzi di memoria AR emory Address Register! il registro dei dati di memoria DR emory Data Register! il contatore di programma Program Conter! il registro della istrzione corrente IR Instrction Register! il registro di stato PSR Program Stats Register 22 Progetto del Datapath: registri Progetto del Datapath: registri Registro degli indirizzi di memoria (AR emory Address Register) indica l'indirizzo della locazione di memoria che si vole selezionare; Registro dei dati di memoria (DR emory Data Register): contiene il dato proveniente dalla locazione di memoria selezionata o il dato che si vole memorizzare nella locazione di memoria selezionata;! Contatore di programma ( Program Conter) ha la fnzione di gidare il flsso della eseczione di n programma, infatti il so contento indica l'indirizzo della prossima istrzione da esegire;! Registro della istrzione corrente (IR Instrction Register) contiene l'istrzione da decodificare ed esegire.! Registro di stato (PSR Program Stats Register) contiene informazioni sllo stato di fnzionamento della macchina. 23 2
Data-path: la ALU Realizzazione del Datapath... Rn Right Operand Left Operand Reslt Fnctional Unit load R3,b load R,c add R3,R store R3,a Stats Registers AR PSR A.L.U. CU RG IR Data Path DR To/from Primary emory Progetto dell nità di controllo Progetto CU: macchina a stati finiti Central Processing Unit (CPU) Data Path Control Unit (CU) Fetch Address Bs Data Bs Decode Primary emory Unit (Eectable emory) Device Interface Eecte
Progetto CU: macchina a stati finiti <- 1 istrzione Progetto CU: macchina a stati finiti fetch decode AR <- DR <- em[ar] IR <- DR <- 1 DECODE = <indirizzo di partenza>; IR = memory[]; haltflag = FALSE; while(haltflag not SET) { eecte(ir); = sizeof(instruct); IR = memory[]; // fetch phase }; HALT? no EXEC eecte si 29 Progetto CU: macchina a stati finiti Le varie piccole operazioni che costitiscono ciascna fase del ciclo, vengono dette microoperazioni (µop) Assmiamo che ciascna µop avvenga in n ciclo di clock AR <- DR <- mem[ar] Progetto CU: macchina a stati finiti L eseczione stessa di na istrzione è decomposta in tante µop Esempio: LOAD 100 AR <- OP(100) DR <- mem[ar] <- DR Nell architettra semplificata che stiamo descrivendo, la lettra di n dato in memoria IR <- DR <- 1 31 32
Progetto CU: macchina a stati finiti Architettre reali: datapath Esempio: STORE 100 AR <- OP(100) DR <- mem[ar] <- DR La scrittra di n dato in memoria costa : (fetch)1(decode)3(eecte)= 8 cicli di clock 33 Architettre reali (IPS) Architettre reali (IPS) PlayStation & Nintendo 6 1. Fetch instrction, from memory @. 2. Increment. 3. Decode instrction.. Fetch operands, from registers or memory. 5. Eecte operation. 6. Store reslt(s), in registers or memory. Instr. emory read1 read2 write data Generalprpose Registers ALU Data emory
Architettre reali (IPS) pipeline Architettre reali (IPS) pipeline 1. Fetch instrction, from memory @, and increment IF 2. Decode instrction, & fetch operands from registers ID 3. Eection operation or calclate memory address EX 1 Stage 1 Stage 2 Stage 3 Stage Stage 5. Read or write memory E 5. Store reslt(s), in registers or memory WB Assmiamo che ciascno stadio tilizzi HW non tilizzato da altri stadi: si ottiene n PARALLELISO DI ESECUZIONE del flsso di controllo Instrctions 2 3 5 1 Stage 1 Stage 2 Stage 3 Stage Stage 5 Stage 1 Stage 2 Stage 3 Stage Stage 5 Stage 1 Stage 2 Stage 3 Stage Stage 5 Stage 1 Stage 2 Stage 3 Stage Stage 5 2 3 5 6 7 8 9 Time Step (Clock Cycle) 37 38 IF stage IF stage ID/EX EX/E ID/EX EX/E E/WB E/WB IF/ID IF/ID <<2 <<2 Instr. emory read1 read2 write data Generalprpose Registers ALU Data emory Instr. emory read1 read2 write data Generalprpose Registers ALU Data emory Sign Et. Sign Et. Fetch instrction, em[]. New = either or branch target of previos instrction. (Case of branch delay slot.) Separate instrction & data memories is a convenient partial lie for diagrams. Accessing memory within 1 cycle is a partial lie. ore later Reslts of each stage mst be stored in registers for net stage. 39 0
ID/EX ID stage EX/E ID/EX ID stage EX/E E/WB E/WB IF/ID IF/ID <<2 <<2 Instr. emory read1 read2 write data Generalprpose Registers ALU Data emory Instr. emory read1 read2 write data Generalprpose Registers ALU Data emory Sign Et. Sign Et. Read 2 sorce registers. (Even if instr. only ses 1.) Compte branch target = Sign-et(address)<<2. Possibly write destination register of earlier instrction. Remember, net instrction eecting IF stage. Store destination register for possible later se. Registers can be read & written in same cycle sch that read old vales. 1 2 ID/EX EX stage EX/E ID/EX E stage EX/E E/WB E/WB IF/ID IF/ID <<2 <<2 Instr. emory read1 read2 write data Generalprpose Registers ALU Data emory Instr. emory read1 read2 write data Generalprpose Registers ALU Data emory Sign Et. Sign Et. Choose which bits specify destination register. Compte arithmetic/logic, address calclation, or condition testing where 2 nd operand is from register or immediate. 3 Possibly read or write memory. Remember, accessing memory within 1 cycle is a partial lie.
ID/EX WB stage EX/E Il problema dell I/O IF/ID E/WB Central Processing Unit (CPU) Instr. emory read1 read2 write data <<2 Sign Et. Generalprpose Registers ALU Zero Data emory Address Bs Data Bs Data Path Control Unit (CU) Primary emory Unit (Eectable emory) Device Interface Possibly write reslt into register. Choose reslt from either memory or ALU. 5 Il problema dell I/O Il problema dell I/O CU AR PSR A.L.U. RG IR DR DATAIN SIN DATAOUT SOUT 8
Il problema dell I/O: modifica del set di istrzioni Il problema dell I/O: l interfaccia HW /SW Possiamo incrementare il nostro set di istrzioni per gestire l I/O Software nella CPU Application Program Abstract I/O achine Device manager programma per gestire il device controller READ Ind Disp em[ind] <- DATAIN Disp WRITE Ind Disp! em[ind] -> DATAOUT Disp Device Controller Device Device Controller Interface... bsy done Error code... Command Stats Data 0 Logic Data 1 Data n-1 bsy done 0 0 idle 0 1 finished 1 0 working Software bsy done 0 0 idle 0 1 finished 1 0 working Hardware Polling I/O // Start the device while((bsy == 1) (done == 1)) wait(); // Device I/O complete done = 0; bsy while((bsy == 0) && (done == 1)) wait(); // Do the I/O operation bsy = 1; done
Step1. SO single-thread Step 2: I/O con interrzioni CPU InterrptReqest flag Device Device Device CPU Interrpt Pending Spporto: macchina HW elementare Device Device Device Anatomia di n interrzione 3 2 5 Interrpt Disk CPU controller controller 1 6 Hardware! Richiesta -> controller -> disco! Disco risponde-> controller->interrpt controller! Interrpt controller -> CPU 1: Interrpt Instrction n Instrction n1 Operating system Interrpt handler Software 3: Retrn odifica del flsso di controllo (Hardware) GESTIONE INTERRUZIONI si si <- 1 istrzione INTERRUPT? AR <- DR <- em[ar] IR <- DR <- 1 DECODE HALT? no no EXEC
odifica del flsso di controllo (Hardware) = <indirizzo di partenza; IR = memory[]; haltflag = FALSE; while(haltflag not SET) { eecte(ir); = sizeof(instruct); IR = memory[]; if(interrptreqest) {/*interrzione*/ memory[0] = ; /*salva all indirizzo 0*/ = memory[1] /*salta all indirizzo 1*/ }; memory[1] contiene l indirizzo dell interrpt handler Step 2: Protezione dello spazio di indirizzamento Address 02ffff 02b000 027fff 023000 01dfff 0 User program and data User program and data Operating system Registri BASE e LIIT Limit Base Address 02ffff 02d000 02bfff 029000 02fff 023000 01dfff 0 User data User data User program Operating system Limit 2 Base 2 Limit 1 Base 1 Step 2. SO mlti-thread Step 3: Dal-ode odalità Utente/Kernel PSR 15 1 13 12 11 10 9 8 7 6 5 3 2 1 0 ode: 1=ser, 0=kernel Enable: 1=on, 0=off ode Enable ode Enable ode Enable Old Prev Cr Total CPU time percentage acchina HW elementare Interrpt controllo elementare dello spazio di indirizzamento Kernel mode time percentage
PSR Step 3: Dal-ode 15 1 13 12 11 10 9 8 7 6 5 3 2 1 0 name nmber information C S ode: 1=ser, 0=kernel Enable: 1=on, 0=off E ode Enable ode Enable Old Prev ode Enable Cr BadVaddr 8 memory address at which address eception occrred Stats 12 interrpt mask and enable bits Case 13 eception type and pending interrpts E(IAR) 1 address of instrction that cased eception B Trappole di sistema operativo (trap): interrpt software Firefo: read(int filedescriptor, void *bffer, int nmbytes) ser mode kernel mode trap a kernel mode trap handler salva registri trova sys_read( ) handler nella vector table sys_read( ) kernel rotine ripristina stato app, ritorna in ser mode 62 resme Trappole di sistema operativo (trap): interrpt software Trappole di sistema operativo (trap): parte hardware trap 1 ode S 2 Branch Table 3 eectetrap(argment) { setode(spervisor); switch(argment) { case 1: = memory[1001]; // Trap handler 1 case 2: = memory[1002]; // Trap handler 2... case n: = memory[1000n];// Trap handler n }; Trsted Code User Spervisor
Step 3: Protezione dello spazio di indirizzamento Step 3: Protezione dello spazio di indirizzamento CPU chip CPU Indirizzi virtali da CPU a U Indirizzi fisici s bs U emory Disk controller Uso della memoria virtale (vista Hardware) Step 3. SO lti-process Il qadro finale acchina HW elementare Interrpt odo K/U U