Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / doscmd / callback.c
CommitLineData
984263bc
MD
1/* No copyright?! */
2
3#include <sys/cdefs.h>
4__FBSDID("$FreeBSD: src/usr.bin/doscmd/callback.c,v 1.2.2.2 2002/04/25 11:04:50 tg Exp $");
5
6#include <sys/queue.h>
7#include "doscmd.h"
8
9/*
10** Callbacks are used for chaining interrupt handlers
11** off interrupt vectors
12*/
13
14struct callback {
15 LIST_ENTRY(callback) chain;
16 u_long vec;
17 callback_t func;
18 const char *name;
19};
20
21LIST_HEAD(cbhead , callback) cbhead[127];
22
23#define CBHASH(x) (((x) * 17) % 127)
24
25/*
26** Register (func) as a handler for (vec)
27*/
28void
29register_callback(u_long vec, callback_t func, const char *name)
30{
31 struct cbhead *head;
32 struct callback *elm;
33
34 elm = malloc(sizeof(struct callback));
35 elm->vec = vec;
36 elm->func = func;
37 elm->name = name;
38
39 head = &cbhead[CBHASH(vec)];
40 LIST_INSERT_HEAD(head, elm, chain);
41}
42
43/*
44** Find a handler for (vec)
45*/
46callback_t
47find_callback(u_long vec)
48{
49 struct cbhead *head;
50 struct callback *elm;
51
52 head = &cbhead[CBHASH(vec)];
53 LIST_FOREACH(elm, head, chain)
54 if (elm->vec == vec)
55 break;
56 if (elm) {
57 debug(D_TRAPS2, "callback %s\n", elm->name);
58 return (elm->func);
59 } else
60 return ((callback_t)0);
61}
62
63u_long trampoline_rover = 0xF1000000;
64
65/*
66 * Interrupts are disabled on an INTn call, so we must restore interrupts
67 * before via STI returning. IRET is not used here because 1) some DOS
68 * calls want to return status via the FLAGS register, and 2) external
69 * routines which hook INTn calls do not always put a FLAGS image on the
70 * stack which re-enables interrupts.
71 */
72u_char softint_trampoline[] = {
73 0xf4, /* HLT */
74 0xfb, /* STI */
75 0xca, /* RETF 2 */
76 2,
77 0,
78};
79u_char hardint_trampoline[] = {
80 0xf4, /* HLT */
81 0xcf, /* IRET */
82};
83u_char null_trampoline[] = {
84 0xcf, /* IRET */
85};
86
87u_long
88insert_generic_trampoline(size_t len, u_char *p)
89{
90 u_char *q;
91 u_long where;
92
93 where = trampoline_rover;
94 q = (u_char *)VECPTR(where);
95 memcpy(q, p, len);
96 trampoline_rover += len;
97 return (where);
98}
99
100u_long
101insert_softint_trampoline(void)
102{
103 return (insert_generic_trampoline(
104 sizeof(softint_trampoline), softint_trampoline));
105}
106
107u_long
108insert_hardint_trampoline(void)
109{
110 return (insert_generic_trampoline(
111 sizeof(hardint_trampoline), hardint_trampoline));
112}
113
114u_long
115insert_null_trampoline(void)
116{
117 return (insert_generic_trampoline(
118 sizeof(null_trampoline), null_trampoline));
119}