cpu.c (3270B)
1 // PingPongOS - PingPong Operating System 2 // © Prof. Carlos A. Maziero, DINF UFPR 3 // Versão 2.1 -- 06/2026 4 5 // ATENÇÃO: ESTE ARQUIVO NÃO DEVE SER ALTERADO; 6 // ALTERAÇÕES SERÃO DESCARTADAS NA CORREÇÃO. 7 8 // Emulação de alguns mecanismos do hardware da CPU 9 10 // padrão de API UNIX a usar (para sigaction) 11 #define _XOPEN_SOURCE 700 12 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include <unistd.h> 16 #include <signal.h> 17 #include <time.h> 18 #include <sys/times.h> 19 #include "cpu.h" 20 21 #define NOERROR 0 22 #define ERROR -1 23 24 // para os sinais 25 static struct sigaction action; 26 27 // para o timer 28 static timer_t timer; 29 static struct itimerspec delay; 30 static struct sigevent sigev; 31 32 //---------------------------------------------------------------------- 33 34 int hw_irq_handle(int irq, void (*handle)(int)) 35 { 36 if ( (irq < 1) || ((irq + SIGRTMIN) >= SIGRTMAX) ) 37 return (ERROR); 38 39 if (handle) 40 { 41 // registra a ação "handle" para o sinal indicado 42 action.sa_handler = handle; 43 sigemptyset(&action.sa_mask); 44 action.sa_flags = SA_NODEFER; 45 if (sigaction(irq + SIGRTMIN, &action, 0) < 0) 46 { 47 perror("IRQ:"); 48 abort(); 49 } 50 } 51 else 52 { 53 // registra a ação default para o sinal indicado 54 action.sa_handler = SIG_DFL; 55 sigemptyset(&action.sa_mask); 56 action.sa_flags = 0; 57 if (sigaction(irq + SIGRTMIN, &action, 0) < 0) 58 { 59 perror("IRQ:"); 60 abort(); 61 } 62 } 63 64 return (NOERROR); 65 } 66 67 //---------------------------------------------------------------------- 68 69 void hw_irq_enable(int enable) 70 { 71 sigset_t mask; 72 73 if (enable) 74 sigemptyset(&mask); 75 else 76 sigfillset(&mask); 77 78 sigprocmask(SIG_SETMASK, &mask, NULL); 79 } 80 81 //---------------------------------------------------------------------- 82 83 int hw_timer(int first, int next) 84 { 85 if (first < 0 || next < 0) 86 return (ERROR); 87 88 // cria o timer 89 sigev.sigev_notify = SIGEV_SIGNAL; 90 sigev.sigev_signo = IRQ_TIMER + SIGRTMIN; 91 sigev.sigev_value.sival_ptr = &timer; 92 if (timer_create(CLOCK_REALTIME, &sigev, &timer) == -1) 93 { 94 perror("TIMER:"); 95 abort(); 96 } 97 98 // configura o timer POSIX 99 delay.it_value.tv_nsec = first * 1000000; 100 delay.it_value.tv_sec = 0; 101 delay.it_interval.tv_nsec = next * 1000000; 102 delay.it_interval.tv_sec = 0; 103 104 // arma o timer POSIX 105 if (timer_settime(timer, 0, &delay, NULL) == -1) 106 { 107 perror("TIMER:"); 108 abort(); 109 } 110 return (NOERROR); 111 } 112 113 //---------------------------------------------------------------------- 114 115 void hw_wfi() 116 { 117 // A syscall pause permite emular o comportamento da instrução HLT 118 // (halt) ou WFI (wait for interrupt), que suspendem a CPU até a 119 // próxima interrupção de hardware, para economizar energia. 120 // Neste caso, pause() suspende este processo até o próximo sinal 121 // UNIX, que emula uma interrupção. 122 pause(); 123 } 124 125 //---------------------------------------------------------------------- 126 127 void hw_poweroff(int error) 128 { 129 // encerra com erro, GDB pode ver o call stack 130 if (error) 131 abort(); 132 133 exit(0); 134 } 135 136 //----------------------------------------------------------------------