aboutsummaryrefslogtreecommitdiff
path: root/sys/compat/ndis/subr_hal.c
blob: 6d9e0c05fa8e522aa71814dc6485a27b02a2e369 (plain) (blame)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * Copyright (c) 2003
 *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Bill Paul.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/types.h>
#include <sys/errno.h>

#include <sys/callout.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/mutex.h>

#include <sys/systm.h>
#include <machine/clock.h>
#include <machine/bus_memio.h>
#include <machine/bus_pio.h>
#include <machine/bus.h>

#include <sys/bus.h>
#include <sys/rman.h>

#include <compat/ndis/pe_var.h>
#include <compat/ndis/hal_var.h>
#include <compat/ndis/ntoskrnl_var.h>

#define __stdcall __attribute__((__stdcall__))
#define FUNC void(*)(void)

__stdcall static void hal_stall_exec_cpu(uint32_t);
__stdcall static void hal_writeport_buf_ulong(uint32_t *,
	uint32_t *, uint32_t);
__stdcall static void hal_writeport_buf_ushort(uint16_t *,
	uint16_t *, uint32_t);
__stdcall static void hal_writeport_buf_uchar(uint8_t *,
	uint8_t *, uint32_t);
__stdcall static void hal_writeport_ulong(uint32_t *, uint32_t);
__stdcall static void hal_writeport_ushort(uint16_t *, uint16_t);
__stdcall static void hal_writeport_uchar(uint8_t *, uint8_t);
__stdcall static uint32_t hal_readport_ulong(uint32_t *);
__stdcall static uint16_t hal_readport_ushort(uint16_t *);
__stdcall static uint8_t hal_readport_uchar(uint8_t *);
__stdcall static void hal_readport_buf_ulong(uint32_t *,
	uint32_t *, uint32_t);
__stdcall static void hal_readport_buf_ushort(uint16_t *,
	uint16_t *, uint32_t);
__stdcall static void hal_readport_buf_uchar(uint8_t *,
	uint8_t *, uint32_t);
__stdcall static uint8_t hal_lock(/*kspin_lock * */void);
__stdcall static void hal_unlock(/*kspin_lock *, uint8_t*/void);
__stdcall static uint8_t hal_irql(void);
__stdcall static void dummy (void);

extern struct mtx_pool *ndis_mtxpool;

__stdcall static void
hal_stall_exec_cpu(usecs)
	uint32_t		usecs;
{
	DELAY(usecs);
	return;
}

__stdcall static void
hal_writeport_ulong(port, val)
	uint32_t		*port;
	uint32_t		val;
{
	bus_space_write_4(I386_BUS_SPACE_IO, 0x0, (uint32_t)port, val);
	return;
}

__stdcall static void
hal_writeport_ushort(port, val)
	uint16_t		*port;
	uint16_t		val;
{
	bus_space_write_2(I386_BUS_SPACE_IO, 0x0, (uint32_t)port, val);
	return;
}

__stdcall static void
hal_writeport_uchar(port, val)
	uint8_t			*port;
	uint8_t			val;
{
	bus_space_write_1(I386_BUS_SPACE_IO, 0x0, (uint32_t)port, val);
	return;
}

__stdcall static void
hal_writeport_buf_ulong(port, val, cnt)
	uint32_t		*port;
	uint32_t		*val;
	uint32_t		cnt;
{
	bus_space_write_multi_4(I386_BUS_SPACE_IO, 0x0,
	    (bus_size_t)port, val, cnt);
	return;
}

__stdcall static void
hal_writeport_buf_ushort(port, val, cnt)
	uint16_t		*port;
	uint16_t		*val;
	uint32_t		cnt;
{
	bus_space_write_multi_2(I386_BUS_SPACE_IO, 0x0,
	    (bus_size_t)port, val, cnt);
	return;
}

__stdcall static void
hal_writeport_buf_uchar(port, val, cnt)
	uint8_t			*port;
	uint8_t			*val;
	uint32_t		cnt;
{
	bus_space_write_multi_1(I386_BUS_SPACE_IO, 0x0,
	    (bus_size_t)port, val, cnt);
	return;
}

__stdcall static uint16_t
hal_readport_ushort(port)
	uint16_t		*port;
{
	return(bus_space_read_2(I386_BUS_SPACE_IO, 0x0, (uint32_t)port));
}

__stdcall static uint32_t
hal_readport_ulong(port)
	uint32_t		*port;
{
	return(bus_space_read_4(I386_BUS_SPACE_IO, 0x0, (uint32_t)port));
}

__stdcall static uint8_t
hal_readport_uchar(port)
	uint8_t			*port;
{
	return(bus_space_read_1(I386_BUS_SPACE_IO, 0x0, (uint32_t)port));
}

__stdcall static void
hal_readport_buf_ulong(port, val, cnt)
	uint32_t		*port;
	uint32_t		*val;
	uint32_t		cnt;
{
	bus_space_read_multi_4(I386_BUS_SPACE_IO, 0x0,
	    (bus_size_t)port, val, cnt);
	return;
}

__stdcall static void
hal_readport_buf_ushort(port, val, cnt)
	uint16_t		*port;
	uint16_t		*val;
	uint32_t		cnt;
{
	bus_space_read_multi_2(I386_BUS_SPACE_IO, 0x0,
	    (bus_size_t)port, val, cnt);
	return;
}

__stdcall static void
hal_readport_buf_uchar(port, val, cnt)
	uint8_t			*port;
	uint8_t			*val;
	uint32_t		cnt;
{
	bus_space_read_multi_1(I386_BUS_SPACE_IO, 0x0,
	    (bus_size_t)port, val, cnt);
	return;
}

__stdcall static uint8_t
hal_lock(/*lock*/void)
{
	kspin_lock		*lock;

	__asm__ __volatile__ ("" : "=c" (lock));

	mtx_pool_lock(ndis_mtxpool, (struct mtx *)*lock);
	return(0);
}

__stdcall static void
hal_unlock(/*lock, newirql*/void)
{
	kspin_lock		*lock;
	uint8_t			newiqrl;

	__asm__ __volatile__ ("" : "=c" (lock), "=d" (newiqrl));

	mtx_pool_unlock(ndis_mtxpool, (struct mtx *)*lock);
	return;
}

__stdcall static uint8_t
hal_irql(void)
{
	return(0);
}

__stdcall
static void dummy()
{
	printf ("hal dummy called...\n");
	return;
}

image_patch_table hal_functbl[] = {
	{ "KeStallExecutionProcessor", (FUNC)hal_stall_exec_cpu },
	{ "WRITE_PORT_ULONG", (FUNC)hal_writeport_ulong },
	{ "WRITE_PORT_USHORT", (FUNC)hal_writeport_ushort },
	{ "WRITE_PORT_UCHAR", (FUNC)hal_writeport_uchar },
	{ "WRITE_PORT_BUFFER_ULONG", (FUNC)hal_writeport_buf_ulong },
	{ "WRITE_PORT_BUFFER_USHORT", (FUNC)hal_writeport_buf_ushort },
	{ "WRITE_PORT_BUFFER_UCHAR", (FUNC)hal_writeport_buf_uchar },
	{ "READ_PORT_ULONG", (FUNC)hal_readport_ulong },
	{ "READ_PORT_USHORT", (FUNC)hal_readport_ushort },
	{ "READ_PORT_UCHAR", (FUNC)hal_readport_uchar },
	{ "READ_PORT_BUFFER_ULONG", (FUNC)hal_readport_buf_ulong },
	{ "READ_PORT_BUFFER_USHORT", (FUNC)hal_readport_buf_ushort },
	{ "READ_PORT_BUFFER_UCHAR", (FUNC)hal_readport_buf_uchar },
	{ "KfAcquireSpinLock", (FUNC)hal_lock },
	{ "KfReleaseSpinLock", (FUNC)hal_unlock },
	{ "KeGetCurrentIrql", (FUNC)hal_irql },

	/*
	 * This last entry is a catch-all for any function we haven't
	 * implemented yet. The PE import list patching routine will
	 * use it for any function that doesn't have an explicit match
	 * in this table.
	 */

	{ NULL, (FUNC)dummy },

	/* End of list. */

	{ NULL, NULL },
};