-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu_interrupt_handler.go
43 lines (41 loc) · 1.09 KB
/
cpu_interrupt_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package emulator
import (
zl "github.com/rs/zerolog"
uc "github.com/unicorn-engine/unicorn/bindings/go/unicorn"
)
type InterruptHandler struct {
mu uc.Unicorn
handler map[uint32]func(uc.Unicorn)
logger zl.Logger
}
func NewInterruptHandler(mu uc.Unicorn) *InterruptHandler {
ih := &InterruptHandler{
mu: mu,
handler: map[uint32]func(uc.Unicorn){},
}
ih.mu.HookAdd(uc.HOOK_INTR, ih.hookInterrupt, 1, 0)
return ih
}
func (ih *InterruptHandler) SetLogger(logger zl.Logger) {
ih.logger = logger
}
func (ih *InterruptHandler) SetHandler(intno uint32, handler func(uc.Unicorn)) {
ih.handler[intno] = handler
}
func (ih *InterruptHandler) hookInterrupt(mu uc.Unicorn, intno uint32) {
cb, exist := ih.handler[intno]
if !exist {
regx, err := mu.RegRead(uc.ARM_REG_PC)
ih.logger.Debug().
Err(err).
Msg("reading reg PC")
ih.logger.Debug().
Uint32("intno", intno).
Uint64("pc", regx).
Msg("unhandled interrupt")
ih.logger.Debug().Err(mu.Stop()).Msg("stopping emulation")
//panic(fmt.Sprintf("Unhandled interrupt %d at %x, stopping emulation", intno, regx))
return
}
cb(mu)
}