]>
wirehaze git hosting - ppos.git/blob - ppos/hardware/cpu.c
1 // PingPongOS - PingPong Operating System
2 // Prof. Carlos A. Maziero, DINF UFPR
3 // Versão 2.0 -- Junho de 2025
5 // ATENÇÃO: ESTE ARQUIVO NÃO DEVE SER ALTERADO;
6 // ALTERAÇÕES SERÃO DESCARTADAS NA CORREÇÃO.
8 // Emula vários mecanismos de uma CPU:
9 // - interrupções virtuais - emuladas com sinais POSIX de tempo real
10 // a partir de SIGRTMIN+1
11 // - desligamento do sistema
13 // padrão de API UNIX a usar (para sigaction)
14 #define _XOPEN_SOURCE 700
21 #include <sys/times.h>
28 static struct sigaction action
;
32 static struct itimerspec delay
;
33 static struct sigevent sigev
;
35 //----------------------------------------------------------------------
37 int hw_irq_handle(int irq
, void (*handle
)(int))
42 if ((irq
+ SIGRTMIN
) >= SIGRTMAX
)
48 // registra a ação para o sinal indicado
49 action
.sa_handler
= handle
;
50 sigemptyset(&action
.sa_mask
);
51 action
.sa_flags
= SA_NODEFER
;
52 if (sigaction(irq
+ SIGRTMIN
, &action
, 0) < 0)
60 //----------------------------------------------------------------------
62 void hw_irq_enable(int enable
)
71 sigprocmask(SIG_SETMASK
, &mask
, NULL
);
74 //----------------------------------------------------------------------
76 int hw_timer(int first
, int next
)
78 if (first
< 0 || next
< 0)
82 sigev
.sigev_notify
= SIGEV_SIGNAL
;
83 sigev
.sigev_signo
= IRQ_TIMER
+ SIGRTMIN
;
84 if (timer_create(CLOCK_REALTIME
, &sigev
, &timer
) == -1)
90 // configura o timer POSIX
91 delay
.it_value
.tv_nsec
= first
* 1000000;
92 delay
.it_value
.tv_sec
= 0;
93 delay
.it_interval
.tv_nsec
= first
* 1000000;
94 delay
.it_interval
.tv_sec
= 0;
97 if (timer_settime(timer
, 0, &delay
, NULL
) == -1)
105 //----------------------------------------------------------------------
109 // A syscall pause permite emular o comportamento da instrução HLT
110 // (halt) ou WFI (wait for interrupt), que suspendem a CPU até a
111 // próxima interrupção de hardware, para economizar energia.
112 // Neste caso, pause() suspende este processo até o próximo sinal
113 // UNIX, que emula uma interrupção.
117 //----------------------------------------------------------------------
119 void hw_poweroff(int error
)
125 //printf("Hardware shutdown, cpu active for %ld ms\n",
126 // 10 * (time.tms_utime + time.tms_stime));
128 // encerra com erro, GDB pode ver o call stack
135 //----------------------------------------------------------------------