aboutsummaryrefslogtreecommitdiffstats
path: root/src/add-ons/kernel/drivers/misc/poke.cpp
blob: bfbcfaefe7d55f45e7105a0272ecf82a8f73f0ab (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * Copyright 2005, Oscar Lesta. All rights reserved.
 * Copyright 2018, Haiku, Inc. All rights reserved.
 * Distributed under the terms of the MIT License.
 */


#include <Drivers.h>
#include <KernelExport.h>
#include <ISA.h>
#include <PCI.h>

#if defined(__i386__) || defined(__x86_64__)
#include <thread.h>
#endif

#include "poke.h"

/*
 TODO: maintain a list of mapped areas in the cookie
 and only allow unmapping them, and clean them up on free.
 */

static status_t poke_open(const char*, uint32, void**);
static status_t poke_close(void*);
static status_t poke_free(void*);
static status_t poke_control(void*, uint32, void*, size_t);
static status_t poke_read(void*, off_t, void*, size_t*);
static status_t poke_write(void*, off_t, const void*, size_t*);


static const char* poke_name[] = {
    "misc/" POKE_DEVICE_NAME,
    NULL
};


device_hooks poke_hooks = {
	poke_open,
	poke_close,
	poke_free,
	poke_control,
	poke_read,
	poke_write,
};

int32 api_version = B_CUR_DRIVER_API_VERSION;

isa_module_info* isa;
pci_module_info* pci;


status_t
init_hardware(void)
{
	return B_OK;
}


status_t
init_driver(void)
{
	if (get_module(B_ISA_MODULE_NAME, (module_info**)&isa) < B_OK)
		return ENOSYS;

	if (get_module(B_PCI_MODULE_NAME, (module_info**)&pci) < B_OK) {
		put_module(B_ISA_MODULE_NAME);
		return ENOSYS;
	}

	return B_OK;
}


void
uninit_driver(void)
{
	put_module(B_ISA_MODULE_NAME);
	put_module(B_PCI_MODULE_NAME);
}


const char**
publish_devices(void)
{
	return poke_name;
}


device_hooks*
find_device(const char* name)
{
	return &poke_hooks;
}


//	#pragma mark -


status_t
poke_open(const char* name, uint32 flags, void** cookie)
{
	*cookie = NULL;

	if (getuid() != 0 && geteuid() != 0)
		return EPERM;

#if defined(__i386__) || defined(__x86_64__)
	/* on x86, raise the IOPL so that outb/inb will work */
	iframe* frame = x86_get_user_iframe();
	int iopl = 3;
	frame->flags &= ~X86_EFLAGS_IO_PRIVILEG_LEVEL;
	frame->flags |= (iopl << X86_EFLAGS_IO_PRIVILEG_LEVEL_SHIFT)
		& X86_EFLAGS_IO_PRIVILEG_LEVEL;
#endif

	return B_OK;
}


status_t
poke_close(void* cookie)
{
	return B_OK;
}


status_t
poke_free(void* cookie)
{
#if defined(__i386__) || defined(__x86_64__)
	iframe* frame = x86_get_user_iframe();
	int iopl = 0;
	frame->flags &= ~X86_EFLAGS_IO_PRIVILEG_LEVEL;
	frame->flags |= (iopl << X86_EFLAGS_IO_PRIVILEG_LEVEL_SHIFT)
		& X86_EFLAGS_IO_PRIVILEG_LEVEL;
#endif

	return B_OK;
}


status_t
poke_control(void* cookie, uint32 op, void* arg, size_t length)
{
	switch (op) {
		case POKE_PORT_READ:
		{
			status_t result = B_OK;
			port_io_args ioctl;
			if (user_memcpy(&ioctl, arg, sizeof(port_io_args)) != B_OK)
				return B_BAD_ADDRESS;
			if (ioctl.signature != POKE_SIGNATURE)
				return B_BAD_VALUE;

			switch (ioctl.size) {
				case 1:
					ioctl.value = isa->read_io_8(ioctl.port);
				break;
				case 2:
					ioctl.value = isa->read_io_16(ioctl.port);
				break;
				case 4:
					ioctl.value = isa->read_io_32(ioctl.port);
				break;
				default:
					result = B_BAD_VALUE;
			}

			if (user_memcpy(arg, &ioctl, sizeof(port_io_args)) != B_OK)
				return B_BAD_ADDRESS;
			return result;
		}

		case POKE_PORT_WRITE:
		{
			status_t result = B_OK;
			port_io_args ioctl;
			if (user_memcpy(&ioctl, arg, sizeof(port_io_args)) != B_OK)
				return B_BAD_ADDRESS;
			if (ioctl.signature != POKE_SIGNATURE)
				return B_BAD_VALUE;

			switch (ioctl.size) {
				case 1:
					isa->write_io_8(ioctl.port, ioctl.value);
					break;
				case 2:
					isa->write_io_16(ioctl.port, ioctl.value);
					break;
				case 4:
					isa->write_io_32(ioctl.port, ioctl.value);
					break;
				default:
					result = B_BAD_VALUE;
			}

			return result;
		}

		case POKE_PORT_INDEXED_READ:
		{
			port_io_args ioctl;
			if (user_memcpy(&ioctl, arg, sizeof(port_io_args)) != B_OK)
				return B_BAD_ADDRESS;
			if (ioctl.signature != POKE_SIGNATURE)
				return B_BAD_VALUE;

			isa->write_io_8(ioctl.port, ioctl.size);
			ioctl.value = isa->read_io_8(ioctl.port + 1);

			if (user_memcpy(arg, &ioctl, sizeof(port_io_args)) != B_OK)
				return B_BAD_ADDRESS;
			return B_OK;
		}

		case POKE_PORT_INDEXED_WRITE:
		{
			port_io_args ioctl;
			if (user_memcpy(&ioctl, arg, sizeof(port_io_args)) != B_OK)
				return B_BAD_ADDRESS;
			if (ioctl.signature != POKE_SIGNATURE)
				return B_BAD_VALUE;

			isa->write_io_8(ioctl.port, ioctl.size);
			isa->write_io_8(ioctl.port + 1, ioctl.value);
			return B_OK;
		}

		case POKE_PCI_READ_CONFIG:
		{
			pci_io_args ioctl;
			if (user_memcpy(&ioctl, arg, sizeof(pci_io_args)) != B_OK)
				return B_BAD_ADDRESS;
			if (ioctl.signature != POKE_SIGNATURE)
				return B_BAD_VALUE;

			ioctl.value = pci->read_pci_config(ioctl.bus, ioctl.device,
				ioctl.function, ioctl.offset, ioctl.size);
			if (user_memcpy(arg, &ioctl, sizeof(pci_io_args)) != B_OK)
				return B_BAD_ADDRESS;
			return B_OK;
		}

		case POKE_PCI_WRITE_CONFIG:
		{
			pci_io_args ioctl;
			if (user_memcpy(&ioctl, arg, sizeof(pci_io_args)) != B_OK)
				return B_BAD_ADDRESS;
			if (ioctl.signature != POKE_SIGNATURE)
				return B_BAD_VALUE;

			pci->write_pci_config(ioctl.bus, ioctl.device, ioctl.function,
				ioctl.offset, ioctl.size, ioctl.value);
			return B_OK;
		}

		case POKE_GET_NTH_PCI_INFO:
		{
			pci_info_args ioctl;
			if (user_memcpy(&ioctl, arg, sizeof(pci_info_args)) != B_OK)
				return B_BAD_ADDRESS;
			if (ioctl.signature != POKE_SIGNATURE)
				return B_BAD_VALUE;

			pci_info info;
			ioctl.status = pci->get_nth_pci_info(ioctl.index, &info);

			if (user_memcpy(ioctl.info, &info, sizeof(pci_info)) != B_OK)
				return B_BAD_ADDRESS;
			if (user_memcpy(arg, &ioctl, sizeof(pci_info_args)) != B_OK)
				return B_BAD_ADDRESS;
			return B_OK;
		}

		case POKE_GET_PHYSICAL_ADDRESS:
		{
			mem_map_args ioctl;
			if (user_memcpy(&ioctl, arg, sizeof(mem_map_args)) != B_OK)
				return B_BAD_ADDRESS;
			physical_entry table;
			status_t result;

			if (ioctl.signature != POKE_SIGNATURE)
				return B_BAD_VALUE;

			result = get_memory_map(ioctl.address, ioctl.size, &table, 1);
			ioctl.physical_address = table.address;
			ioctl.size = table.size;
			if (user_memcpy(arg, &ioctl, sizeof(mem_map_args)) != B_OK)
				return B_BAD_ADDRESS;
			return result;
		}

		case POKE_MAP_MEMORY:
		{
			mem_map_args ioctl;
			if (user_memcpy(&ioctl, arg, sizeof(mem_map_args)) != B_OK)
				return B_BAD_ADDRESS;
			if (ioctl.signature != POKE_SIGNATURE)
				return B_BAD_VALUE;

			char name[B_OS_NAME_LENGTH];
			if (user_strlcpy(name, ioctl.name, B_OS_NAME_LENGTH) != B_OK)
				return B_BAD_ADDRESS;

			ioctl.area = map_physical_memory(name,
				(addr_t)ioctl.physical_address, ioctl.size, ioctl.flags,
				ioctl.protection, (void**)&ioctl.address);

			if (user_memcpy(arg, &ioctl, sizeof(mem_map_args)) != B_OK)
				return B_BAD_ADDRESS;
			return ioctl.area;
		}

		case POKE_UNMAP_MEMORY:
		{
			mem_map_args ioctl;
			if (user_memcpy(&ioctl, arg, sizeof(mem_map_args)) != B_OK)
				return B_BAD_ADDRESS;
			if (ioctl.signature != POKE_SIGNATURE)
				return B_BAD_VALUE;

			return delete_area(ioctl.area);
		}
	}

	return B_BAD_VALUE;
}


status_t
poke_read(void* cookie, off_t position, void* buffer, size_t* numBytes)
{
	*numBytes = 0;
	return B_NOT_ALLOWED;
}


status_t
poke_write(void* cookie, off_t position, const void* buffer, size_t* numBytes)
{
	*numBytes = 0;
	return B_NOT_ALLOWED;
}