xref: /freebsd-13-stable/sys/compat/ndis/ntoskrnl_var.h (revision f8167e0404dab9ffeaca95853dd237ab7c587f82)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 2003
5  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef _NTOSKRNL_VAR_H_
36 #define	_NTOSKRNL_VAR_H_
37 
38 #define	MTX_NTOSKRNL_SPIN_LOCK "NDIS thread lock"
39 
40 /*
41  * us_buf is really a wchar_t *, but it's inconvenient to include
42  * all the necessary header goop needed to define it, and it's a
43  * pointer anyway, so for now, just make it a uint16_t *.
44  */
45 struct unicode_string {
46 	uint16_t		us_len;
47 	uint16_t		us_maxlen;
48 	uint16_t		*us_buf;
49 };
50 
51 typedef struct unicode_string unicode_string;
52 
53 struct ansi_string {
54 	uint16_t		as_len;
55 	uint16_t		as_maxlen;
56 	char			*as_buf;
57 };
58 
59 typedef struct ansi_string ansi_string;
60 
61 /*
62  * Windows memory descriptor list. In Windows, it's possible for
63  * buffers to be passed between user and kernel contexts without
64  * copying. Buffers may also be allocated in either paged or
65  * non-paged memory regions. An MDL describes the pages of memory
66  * used to contain a particular buffer. Note that a single MDL
67  * may describe a buffer that spans multiple pages. An array of
68  * page addresses appears immediately after the MDL structure itself.
69  * MDLs are therefore implicitly variably sized, even though they
70  * don't look it.
71  *
72  * Note that in FreeBSD, we can take many shortcuts in the way
73  * we handle MDLs because:
74  *
75  * - We are only concerned with pages in kernel context. This means
76  *   we will only ever use the kernel's memory map, and remapping
77  *   of buffers is never needed.
78  *
79  * - Kernel pages can never be paged out, so we don't have to worry
80  *   about whether or not a page is actually mapped before going to
81  *   touch it.
82  */
83 
84 struct mdl {
85 	struct mdl		*mdl_next;
86 	uint16_t		mdl_size;
87 	uint16_t		mdl_flags;
88 	void			*mdl_process;
89 	void			*mdl_mappedsystemva;
90 	void			*mdl_startva;
91 	uint32_t		mdl_bytecount;
92 	uint32_t		mdl_byteoffset;
93 };
94 
95 typedef struct mdl mdl, ndis_buffer;
96 
97 /* MDL flags */
98 
99 #define	MDL_MAPPED_TO_SYSTEM_VA		0x0001
100 #define	MDL_PAGES_LOCKED		0x0002
101 #define	MDL_SOURCE_IS_NONPAGED_POOL	0x0004
102 #define	MDL_ALLOCATED_FIXED_SIZE	0x0008
103 #define	MDL_PARTIAL			0x0010
104 #define	MDL_PARTIAL_HAS_BEEN_MAPPED	0x0020
105 #define	MDL_IO_PAGE_READ		0x0040
106 #define	MDL_WRITE_OPERATION		0x0080
107 #define	MDL_PARENT_MAPPED_SYSTEM_VA	0x0100
108 #define	MDL_FREE_EXTRA_PTES		0x0200
109 #define	MDL_IO_SPACE			0x0800
110 #define	MDL_NETWORK_HEADER		0x1000
111 #define	MDL_MAPPING_CAN_FAIL		0x2000
112 #define	MDL_ALLOCATED_MUST_SUCCEED	0x4000
113 #define	MDL_ZONE_ALLOCED		0x8000	/* BSD private */
114 
115 #define	MDL_ZONE_PAGES 16
116 #define	MDL_ZONE_SIZE (sizeof(mdl) + (sizeof(vm_offset_t) * MDL_ZONE_PAGES))
117 
118 /* Note: assumes x86 page size of 4K. */
119 
120 #ifndef PAGE_SHIFT
121 #if PAGE_SIZE == 4096
122 #define	PAGE_SHIFT	12
123 #elif PAGE_SIZE == 8192
124 #define	PAGE_SHIFT	13
125 #else
126 #error PAGE_SHIFT undefined!
127 #endif
128 #endif
129 
130 #define	SPAN_PAGES(ptr, len)					\
131 	((uint32_t)((((uintptr_t)(ptr) & (PAGE_SIZE - 1)) +	\
132 	(len) + (PAGE_SIZE - 1)) >> PAGE_SHIFT))
133 
134 #define	PAGE_ALIGN(ptr)						\
135 	((void *)((uintptr_t)(ptr) & ~(PAGE_SIZE - 1)))
136 
137 #define	BYTE_OFFSET(ptr)					\
138 	((uint32_t)((uintptr_t)(ptr) & (PAGE_SIZE - 1)))
139 
140 #define	MDL_PAGES(m)	(vm_offset_t *)(m + 1)
141 
142 #define	MmInitializeMdl(b, baseva, len)					\
143 	(b)->mdl_next = NULL;						\
144 	(b)->mdl_size = (uint16_t)(sizeof(mdl) +			\
145 		(sizeof(vm_offset_t) * SPAN_PAGES((baseva), (len))));	\
146 	(b)->mdl_flags = 0;						\
147 	(b)->mdl_startva = (void *)PAGE_ALIGN((baseva));		\
148 	(b)->mdl_byteoffset = BYTE_OFFSET((baseva));			\
149 	(b)->mdl_bytecount = (uint32_t)(len);
150 
151 #define	MmGetMdlByteOffset(mdl)		((mdl)->mdl_byteoffset)
152 #define	MmGetMdlByteCount(mdl)		((mdl)->mdl_bytecount)
153 #define	MmGetMdlVirtualAddress(mdl)					\
154 	((void *)((char *)((mdl)->mdl_startva) + (mdl)->mdl_byteoffset))
155 #define	MmGetMdlStartVa(mdl)		((mdl)->mdl_startva)
156 #define	MmGetMdlPfnArray(mdl)		MDL_PAGES(mdl)
157 
158 #define	WDM_MAJOR		1
159 #define	WDM_MINOR_WIN98		0x00
160 #define	WDM_MINOR_WINME		0x05
161 #define	WDM_MINOR_WIN2000	0x10
162 #define	WDM_MINOR_WINXP		0x20
163 #define	WDM_MINOR_WIN2003	0x30
164 
165 enum nt_caching_type {
166 	MmNonCached			= 0,
167 	MmCached			= 1,
168 	MmWriteCombined			= 2,
169 	MmHardwareCoherentCached	= 3,
170 	MmNonCachedUnordered		= 4,
171 	MmUSWCCached			= 5,
172 	MmMaximumCacheType		= 6
173 };
174 
175 /*-
176  * The ndis_kspin_lock type is called KSPIN_LOCK in MS-Windows.
177  * According to the Windows DDK header files, KSPIN_LOCK is defined like this:
178  *	typedef ULONG_PTR KSPIN_LOCK;
179  *
180  * From basetsd.h (SDK, Feb. 2003):
181  *	typedef [public] unsigned __int3264 ULONG_PTR, *PULONG_PTR;
182  *	typedef unsigned __int64 ULONG_PTR, *PULONG_PTR;
183  *	typedef _W64 unsigned long ULONG_PTR, *PULONG_PTR;
184  *
185  * The keyword __int3264 specifies an integral type that has the following
186  * properties:
187  *	+ It is 32-bit on 32-bit platforms
188  *	+ It is 64-bit on 64-bit platforms
189  *	+ It is 32-bit on the wire for backward compatibility.
190  *	  It gets truncated on the sending side and extended appropriately
191  *	  (signed or unsigned) on the receiving side.
192  *
193  * Thus register_t seems the proper mapping onto FreeBSD for spin locks.
194  */
195 
196 typedef register_t kspin_lock;
197 
198 struct slist_entry {
199 	struct slist_entry	*sl_next;
200 };
201 
202 typedef struct slist_entry slist_entry;
203 
204 union slist_header {
205 	uint64_t		slh_align;
206 	struct {
207 		struct slist_entry	*slh_next;
208 		uint16_t		slh_depth;
209 		uint16_t		slh_seq;
210 	} slh_list;
211 };
212 
213 typedef union slist_header slist_header;
214 
215 struct list_entry {
216 	struct list_entry *nle_flink;
217 	struct list_entry *nle_blink;
218 };
219 
220 typedef struct list_entry list_entry;
221 
222 #define	InitializeListHead(l)			\
223 	(l)->nle_flink = (l)->nle_blink = (l)
224 
225 #define	IsListEmpty(h)				\
226 	((h)->nle_flink == (h))
227 
228 #define	RemoveEntryList(e)			\
229 	do {					\
230 		list_entry		*b;	\
231 		list_entry		*f;	\
232 						\
233 		f = (e)->nle_flink;		\
234 		b = (e)->nle_blink;		\
235 		b->nle_flink = f;		\
236 		f->nle_blink = b;		\
237 	} while (0)
238 
239 /* These two have to be inlined since they return things. */
240 
241 static __inline__ list_entry *
RemoveHeadList(list_entry * l)242 RemoveHeadList(list_entry *l)
243 {
244 	list_entry		*f;
245 	list_entry		*e;
246 
247 	e = l->nle_flink;
248 	f = e->nle_flink;
249 	l->nle_flink = f;
250 	f->nle_blink = l;
251 
252 	return (e);
253 }
254 
255 static __inline__ list_entry *
RemoveTailList(list_entry * l)256 RemoveTailList(list_entry *l)
257 {
258 	list_entry		*b;
259 	list_entry		*e;
260 
261 	e = l->nle_blink;
262 	b = e->nle_blink;
263 	l->nle_blink = b;
264 	b->nle_flink = l;
265 
266 	return (e);
267 }
268 
269 #define	InsertTailList(l, e)			\
270 	do {					\
271 		list_entry		*b;	\
272 						\
273 		b = l->nle_blink;		\
274 		e->nle_flink = l;		\
275 		e->nle_blink = b;		\
276 		b->nle_flink = (e);		\
277 		l->nle_blink = (e);		\
278 	} while (0)
279 
280 #define	InsertHeadList(l, e)			\
281 	do {					\
282 		list_entry		*f;	\
283 						\
284 		f = l->nle_flink;		\
285 		e->nle_flink = f;		\
286 		e->nle_blink = l;		\
287 		f->nle_blink = e;		\
288 		l->nle_flink = e;		\
289 	} while (0)
290 
291 #define	CONTAINING_RECORD(addr, type, field)	\
292 	((type *)((vm_offset_t)(addr) - (vm_offset_t)(&((type *)0)->field)))
293 
294 struct nt_dispatch_header {
295 	uint8_t			dh_type;
296 	uint8_t			dh_abs;
297 	uint8_t			dh_size;
298 	uint8_t			dh_inserted;
299 	int32_t			dh_sigstate;
300 	list_entry		dh_waitlisthead;
301 };
302 
303 typedef struct nt_dispatch_header nt_dispatch_header;
304 
305 /* Dispatcher object types */
306 
307 #define	DISP_TYPE_NOTIFICATION_EVENT	0	/* KEVENT */
308 #define	DISP_TYPE_SYNCHRONIZATION_EVENT	1	/* KEVENT */
309 #define	DISP_TYPE_MUTANT		2	/* KMUTANT/KMUTEX */
310 #define	DISP_TYPE_PROCESS		3	/* KPROCESS */
311 #define	DISP_TYPE_QUEUE			4	/* KQUEUE */
312 #define	DISP_TYPE_SEMAPHORE		5	/* KSEMAPHORE */
313 #define	DISP_TYPE_THREAD		6	/* KTHREAD */
314 #define	DISP_TYPE_NOTIFICATION_TIMER	8	/* KTIMER */
315 #define	DISP_TYPE_SYNCHRONIZATION_TIMER	9	/* KTIMER */
316 
317 #define	OTYPE_EVENT		0
318 #define	OTYPE_MUTEX		1
319 #define	OTYPE_THREAD		2
320 #define	OTYPE_TIMER		3
321 
322 /* Windows dispatcher levels. */
323 
324 #define	PASSIVE_LEVEL		0
325 #define	LOW_LEVEL		0
326 #define	APC_LEVEL		1
327 #define	DISPATCH_LEVEL		2
328 #define	DEVICE_LEVEL		(DISPATCH_LEVEL + 1)
329 #define	PROFILE_LEVEL		27
330 #define	CLOCK1_LEVEL		28
331 #define	CLOCK2_LEVEL		28
332 #define	IPI_LEVEL		29
333 #define	POWER_LEVEL		30
334 #define	HIGH_LEVEL		31
335 
336 #define	SYNC_LEVEL_UP		DISPATCH_LEVEL
337 #define	SYNC_LEVEL_MP		(IPI_LEVEL - 1)
338 
339 #define	AT_PASSIVE_LEVEL(td)		\
340 	((td)->td_proc->p_flag & P_KPROC == FALSE)
341 
342 #define	AT_DISPATCH_LEVEL(td)		\
343 	((td)->td_base_pri == PI_REALTIME)
344 
345 #define	AT_DIRQL_LEVEL(td)		\
346 	((td)->td_priority <= PI_NET)
347 
348 #define	AT_HIGH_LEVEL(td)		\
349 	((td)->td_critnest != 0)
350 
351 struct nt_objref {
352 	nt_dispatch_header	no_dh;
353 	void			*no_obj;
354 	TAILQ_ENTRY(nt_objref)	link;
355 };
356 
357 TAILQ_HEAD(nt_objref_head, nt_objref);
358 
359 typedef struct nt_objref nt_objref;
360 
361 #define	EVENT_TYPE_NOTIFY	0
362 #define	EVENT_TYPE_SYNC		1
363 
364 struct ktimer {
365 	nt_dispatch_header	k_header;
366 	uint64_t		k_duetime;
367 	union {
368 		list_entry		k_timerlistentry;
369 		struct callout		*k_callout;
370 	} u;
371 	void			*k_dpc;
372 	uint32_t		k_period;
373 };
374 
375 #define	k_timerlistentry	u.k_timerlistentry
376 #define	k_callout		u.k_callout
377 
378 typedef struct ktimer ktimer;
379 
380 struct nt_kevent {
381 	nt_dispatch_header	k_header;
382 };
383 
384 typedef struct nt_kevent nt_kevent;
385 
386 /* Kernel defered procedure call (i.e. timer callback) */
387 
388 struct kdpc;
389 typedef void (*kdpc_func)(struct kdpc *, void *, void *, void *);
390 
391 struct kdpc {
392 	uint16_t		k_type;
393 	uint8_t			k_num;		/* CPU number */
394 	uint8_t			k_importance;	/* priority */
395 	list_entry		k_dpclistentry;
396 	void			*k_deferedfunc;
397 	void			*k_deferredctx;
398 	void			*k_sysarg1;
399 	void			*k_sysarg2;
400 	void			*k_lock;
401 };
402 
403 #define	KDPC_IMPORTANCE_LOW	0
404 #define	KDPC_IMPORTANCE_MEDIUM	1
405 #define	KDPC_IMPORTANCE_HIGH	2
406 
407 #define	KDPC_CPU_DEFAULT	255
408 
409 typedef struct kdpc kdpc;
410 
411 /*
412  * Note: the acquisition count is BSD-specific. The Microsoft
413  * documentation says that mutexes can be acquired recursively
414  * by a given thread, but that you must release the mutex as
415  * many times as you acquired it before it will be set to the
416  * signalled state (i.e. before any other threads waiting on
417  * the object will be woken up). However the Windows KMUTANT
418  * structure has no field for keeping track of the number of
419  * acquisitions, so we need to add one ourselves. As long as
420  * driver code treats the mutex as opaque, we should be ok.
421  */
422 struct kmutant {
423 	nt_dispatch_header	km_header;
424 	list_entry		km_listentry;
425 	void			*km_ownerthread;
426 	uint8_t			km_abandoned;
427 	uint8_t			km_apcdisable;
428 };
429 
430 typedef struct kmutant kmutant;
431 
432 #define	LOOKASIDE_DEPTH 256
433 
434 struct general_lookaside {
435 	slist_header		gl_listhead;
436 	uint16_t		gl_depth;
437 	uint16_t		gl_maxdepth;
438 	uint32_t		gl_totallocs;
439 	union {
440 		uint32_t		gl_allocmisses;
441 		uint32_t		gl_allochits;
442 	} u_a;
443 	uint32_t		gl_totalfrees;
444 	union {
445 		uint32_t		gl_freemisses;
446 		uint32_t		gl_freehits;
447 	} u_m;
448 	uint32_t		gl_type;
449 	uint32_t		gl_tag;
450 	uint32_t		gl_size;
451 	void			*gl_allocfunc;
452 	void			*gl_freefunc;
453 	list_entry		gl_listent;
454 	uint32_t		gl_lasttotallocs;
455 	union {
456 		uint32_t		gl_lastallocmisses;
457 		uint32_t		gl_lastallochits;
458 	} u_l;
459 	uint32_t		gl_rsvd[2];
460 };
461 
462 typedef struct general_lookaside general_lookaside;
463 
464 struct npaged_lookaside_list {
465 	general_lookaside	nll_l;
466 #ifdef __i386__
467 	kspin_lock		nll_obsoletelock;
468 #endif
469 };
470 
471 typedef struct npaged_lookaside_list npaged_lookaside_list;
472 typedef struct npaged_lookaside_list paged_lookaside_list;
473 
474 typedef void * (*lookaside_alloc_func)(uint32_t, size_t, uint32_t);
475 typedef void (*lookaside_free_func)(void *);
476 
477 struct irp;
478 
479 struct kdevice_qentry {
480 	list_entry		kqe_devlistent;
481 	uint32_t		kqe_sortkey;
482 	uint8_t			kqe_inserted;
483 };
484 
485 typedef struct kdevice_qentry kdevice_qentry;
486 
487 struct kdevice_queue {
488 	uint16_t		kq_type;
489 	uint16_t		kq_size;
490 	list_entry		kq_devlisthead;
491 	kspin_lock		kq_lock;
492 	uint8_t			kq_busy;
493 };
494 
495 typedef struct kdevice_queue kdevice_queue;
496 
497 struct wait_ctx_block {
498 	kdevice_qentry		wcb_waitqueue;
499 	void			*wcb_devfunc;
500 	void			*wcb_devctx;
501 	uint32_t		wcb_mapregcnt;
502 	void			*wcb_devobj;
503 	void			*wcb_curirp;
504 	void			*wcb_bufchaindpc;
505 };
506 
507 typedef struct wait_ctx_block wait_ctx_block;
508 
509 struct wait_block {
510 	list_entry		wb_waitlist;
511 	void			*wb_kthread;
512 	nt_dispatch_header	*wb_object;
513 	struct wait_block	*wb_next;
514 #ifdef notdef
515 	uint16_t		wb_waitkey;
516 	uint16_t		wb_waittype;
517 #endif
518 	uint8_t			wb_waitkey;
519 	uint8_t			wb_waittype;
520 	uint8_t			wb_awakened;
521 	uint8_t			wb_oldpri;
522 };
523 
524 typedef struct wait_block wait_block;
525 
526 #define	wb_ext wb_kthread
527 
528 #define	THREAD_WAIT_OBJECTS	3
529 #define	MAX_WAIT_OBJECTS	64
530 
531 #define	WAITTYPE_ALL		0
532 #define	WAITTYPE_ANY		1
533 
534 #define	WAITKEY_VALID		0x8000
535 
536 /* kthread priority  */
537 #define	LOW_PRIORITY		0
538 #define	LOW_REALTIME_PRIORITY	16
539 #define	HIGH_PRIORITY		31
540 
541 struct thread_context {
542 	void			*tc_thrctx;
543 	void			*tc_thrfunc;
544 };
545 
546 typedef struct thread_context thread_context;
547 
548 /* Forward declaration */
549 struct driver_object;
550 struct devobj_extension;
551 
552 struct driver_extension {
553 	struct driver_object	*dre_driverobj;
554 	void			*dre_adddevicefunc;
555 	uint32_t		dre_reinitcnt;
556 	unicode_string		dre_srvname;
557 
558 	/*
559 	 * Drivers are allowed to add one or more custom extensions
560 	 * to the driver object, but there's no special pointer
561 	 * for them. Hang them off here for now.
562 	 */
563 
564 	list_entry		dre_usrext;
565 };
566 
567 typedef struct driver_extension driver_extension;
568 
569 struct custom_extension {
570 	list_entry		ce_list;
571 	void			*ce_clid;
572 };
573 
574 typedef struct custom_extension custom_extension;
575 
576 /*
577  * The KINTERRUPT structure in Windows is opaque to drivers.
578  * We define our own custom version with things we need.
579  */
580 
581 struct kinterrupt {
582 	list_entry		ki_list;
583 	device_t		ki_dev;
584 	int			ki_rid;
585 	void			*ki_cookie;
586 	struct resource		*ki_irq;
587 	kspin_lock		ki_lock_priv;
588 	kspin_lock		*ki_lock;
589 	void			*ki_svcfunc;
590 	void			*ki_svcctx;
591 };
592 
593 typedef struct kinterrupt kinterrupt;
594 
595 struct ksystem_time {
596 	uint32_t	low_part;
597 	int32_t		high1_time;
598 	int32_t		high2_time;
599 };
600 
601 enum nt_product_type {
602 	NT_PRODUCT_WIN_NT = 1,
603 	NT_PRODUCT_LAN_MAN_NT,
604 	NT_PRODUCT_SERVER
605 };
606 
607 enum alt_arch_type {
608 	STANDARD_DESIGN,
609 	NEC98x86,
610 	END_ALTERNATIVES
611 };
612 
613 struct kuser_shared_data {
614 	uint32_t		tick_count;
615 	uint32_t		tick_count_multiplier;
616 	volatile struct		ksystem_time interrupt_time;
617 	volatile struct		ksystem_time system_time;
618 	volatile struct		ksystem_time time_zone_bias;
619 	uint16_t		image_number_low;
620 	uint16_t		image_number_high;
621 	int16_t			nt_system_root[260];
622 	uint32_t		max_stack_trace_depth;
623 	uint32_t		crypto_exponent;
624 	uint32_t		time_zone_id;
625 	uint32_t		large_page_min;
626 	uint32_t		reserved2[7];
627 	enum nt_product_type	nt_product_type;
628 	uint8_t			product_type_is_valid;
629 	uint32_t		nt_major_version;
630 	uint32_t		nt_minor_version;
631 	uint8_t			processor_features[64];
632 	uint32_t		reserved1;
633 	uint32_t		reserved3;
634 	volatile uint32_t	time_slip;
635 	enum alt_arch_type	alt_arch_type;
636 	int64_t			system_expiration_date;
637 	uint32_t		suite_mask;
638 	uint8_t			kdbg_enabled;
639 	volatile uint32_t	active_console;
640 	volatile uint32_t	dismount_count;
641 	uint32_t		com_plus_package;
642 	uint32_t		last_system_rit_event_tick_count;
643 	uint32_t		num_phys_pages;
644 	uint8_t			safe_boot_mode;
645 	uint32_t		trace_log;
646 	uint64_t		fill0;
647 	uint64_t		sys_call[4];
648 	union {
649 		volatile struct	ksystem_time	tick_count;
650 		volatile uint64_t		tick_count_quad;
651 	} tick;
652 };
653 
654 /*
655  * In Windows, there are Physical Device Objects (PDOs) and
656  * Functional Device Objects (FDOs). Physical Device Objects are
657  * created and maintained by bus drivers. For example, the PCI
658  * bus driver might detect two PCI ethernet cards on a given
659  * bus. The PCI bus driver will then allocate two device_objects
660  * for its own internal bookeeping purposes. This is analogous
661  * to the device_t that the FreeBSD PCI code allocates and passes
662  * into each PCI driver's probe and attach routines.
663  *
664  * When an ethernet driver claims one of the ethernet cards
665  * on the bus, it will create its own device_object. This is
666  * the Functional Device Object. This object is analogous to the
667  * device-specific softc structure.
668  */
669 
670 struct device_object {
671 	uint16_t		do_type;
672 	uint16_t		do_size;
673 	uint32_t		do_refcnt;
674 	struct driver_object	*do_drvobj;
675 	struct device_object	*do_nextdev;
676 	struct device_object	*do_attacheddev;
677 	struct irp		*do_currirp;
678 	void			*do_iotimer;
679 	uint32_t		do_flags;
680 	uint32_t		do_characteristics;
681 	void			*do_vpb;
682 	void			*do_devext;
683 	uint32_t		do_devtype;
684 	uint8_t			do_stacksize;
685 	union {
686 		list_entry		do_listent;
687 		wait_ctx_block		do_wcb;
688 	} queue;
689 	uint32_t		do_alignreq;
690 	kdevice_queue		do_devqueue;
691 	struct kdpc		do_dpc;
692 	uint32_t		do_activethreads;
693 	void			*do_securitydesc;
694 	struct nt_kevent	do_devlock;
695 	uint16_t		do_sectorsz;
696 	uint16_t		do_spare1;
697 	struct devobj_extension	*do_devobj_ext;
698 	void			*do_rsvd;
699 };
700 
701 typedef struct device_object device_object;
702 
703 struct devobj_extension {
704 	uint16_t		dve_type;
705 	uint16_t		dve_size;
706 	device_object		*dve_devobj;
707 };
708 
709 typedef struct devobj_extension devobj_extension;
710 
711 /* Device object flags */
712 
713 #define	DO_VERIFY_VOLUME		0x00000002
714 #define	DO_BUFFERED_IO			0x00000004
715 #define	DO_EXCLUSIVE			0x00000008
716 #define	DO_DIRECT_IO			0x00000010
717 #define	DO_MAP_IO_BUFFER		0x00000020
718 #define	DO_DEVICE_HAS_NAME		0x00000040
719 #define	DO_DEVICE_INITIALIZING		0x00000080
720 #define	DO_SYSTEM_BOOT_PARTITION	0x00000100
721 #define	DO_LONG_TERM_REQUESTS		0x00000200
722 #define	DO_NEVER_LAST_DEVICE		0x00000400
723 #define	DO_SHUTDOWN_REGISTERED		0x00000800
724 #define	DO_BUS_ENUMERATED_DEVICE	0x00001000
725 #define	DO_POWER_PAGABLE		0x00002000
726 #define	DO_POWER_INRUSH			0x00004000
727 #define	DO_LOW_PRIORITY_FILESYSTEM	0x00010000
728 
729 /* Priority boosts */
730 
731 #define	IO_NO_INCREMENT			0
732 #define	IO_CD_ROM_INCREMENT		1
733 #define	IO_DISK_INCREMENT		1
734 #define	IO_KEYBOARD_INCREMENT		6
735 #define	IO_MAILSLOT_INCREMENT		2
736 #define	IO_MOUSE_INCREMENT		6
737 #define	IO_NAMED_PIPE_INCREMENT		2
738 #define	IO_NETWORK_INCREMENT		2
739 #define	IO_PARALLEL_INCREMENT		1
740 #define	IO_SERIAL_INCREMENT		2
741 #define	IO_SOUND_INCREMENT		8
742 #define	IO_VIDEO_INCREMENT		1
743 
744 /* IRP major codes */
745 
746 #define	IRP_MJ_CREATE                   0x00
747 #define	IRP_MJ_CREATE_NAMED_PIPE        0x01
748 #define	IRP_MJ_CLOSE                    0x02
749 #define	IRP_MJ_READ                     0x03
750 #define	IRP_MJ_WRITE                    0x04
751 #define	IRP_MJ_QUERY_INFORMATION        0x05
752 #define	IRP_MJ_SET_INFORMATION          0x06
753 #define	IRP_MJ_QUERY_EA                 0x07
754 #define	IRP_MJ_SET_EA                   0x08
755 #define	IRP_MJ_FLUSH_BUFFERS            0x09
756 #define	IRP_MJ_QUERY_VOLUME_INFORMATION 0x0a
757 #define	IRP_MJ_SET_VOLUME_INFORMATION   0x0b
758 #define	IRP_MJ_DIRECTORY_CONTROL        0x0c
759 #define	IRP_MJ_FILE_SYSTEM_CONTROL      0x0d
760 #define	IRP_MJ_DEVICE_CONTROL           0x0e
761 #define	IRP_MJ_INTERNAL_DEVICE_CONTROL  0x0f
762 #define	IRP_MJ_SHUTDOWN                 0x10
763 #define	IRP_MJ_LOCK_CONTROL             0x11
764 #define	IRP_MJ_CLEANUP                  0x12
765 #define	IRP_MJ_CREATE_MAILSLOT          0x13
766 #define	IRP_MJ_QUERY_SECURITY           0x14
767 #define	IRP_MJ_SET_SECURITY             0x15
768 #define	IRP_MJ_POWER                    0x16
769 #define	IRP_MJ_SYSTEM_CONTROL           0x17
770 #define	IRP_MJ_DEVICE_CHANGE            0x18
771 #define	IRP_MJ_QUERY_QUOTA              0x19
772 #define	IRP_MJ_SET_QUOTA                0x1a
773 #define	IRP_MJ_PNP                      0x1b
774 #define	IRP_MJ_PNP_POWER                IRP_MJ_PNP      // Obsolete....
775 #define	IRP_MJ_MAXIMUM_FUNCTION         0x1b
776 #define	IRP_MJ_SCSI                     IRP_MJ_INTERNAL_DEVICE_CONTROL
777 
778 /* IRP minor codes */
779 
780 #define	IRP_MN_QUERY_DIRECTORY          0x01
781 #define	IRP_MN_NOTIFY_CHANGE_DIRECTORY  0x02
782 #define	IRP_MN_USER_FS_REQUEST          0x00
783 
784 #define	IRP_MN_MOUNT_VOLUME             0x01
785 #define	IRP_MN_VERIFY_VOLUME            0x02
786 #define	IRP_MN_LOAD_FILE_SYSTEM         0x03
787 #define	IRP_MN_TRACK_LINK               0x04
788 #define	IRP_MN_KERNEL_CALL              0x04
789 
790 #define	IRP_MN_LOCK                     0x01
791 #define	IRP_MN_UNLOCK_SINGLE            0x02
792 #define	IRP_MN_UNLOCK_ALL               0x03
793 #define	IRP_MN_UNLOCK_ALL_BY_KEY        0x04
794 
795 #define	IRP_MN_NORMAL                   0x00
796 #define	IRP_MN_DPC                      0x01
797 #define	IRP_MN_MDL                      0x02
798 #define	IRP_MN_COMPLETE                 0x04
799 #define	IRP_MN_COMPRESSED               0x08
800 
801 #define	IRP_MN_MDL_DPC                  (IRP_MN_MDL | IRP_MN_DPC)
802 #define	IRP_MN_COMPLETE_MDL             (IRP_MN_COMPLETE | IRP_MN_MDL)
803 #define	IRP_MN_COMPLETE_MDL_DPC         (IRP_MN_COMPLETE_MDL | IRP_MN_DPC)
804 
805 #define	IRP_MN_SCSI_CLASS               0x01
806 
807 #define	IRP_MN_START_DEVICE                 0x00
808 #define	IRP_MN_QUERY_REMOVE_DEVICE          0x01
809 #define	IRP_MN_REMOVE_DEVICE                0x02
810 #define	IRP_MN_CANCEL_REMOVE_DEVICE         0x03
811 #define	IRP_MN_STOP_DEVICE                  0x04
812 #define	IRP_MN_QUERY_STOP_DEVICE            0x05
813 #define	IRP_MN_CANCEL_STOP_DEVICE           0x06
814 
815 #define	IRP_MN_QUERY_DEVICE_RELATIONS       0x07
816 #define	IRP_MN_QUERY_INTERFACE              0x08
817 #define	IRP_MN_QUERY_CAPABILITIES           0x09
818 #define	IRP_MN_QUERY_RESOURCES              0x0A
819 #define	IRP_MN_QUERY_RESOURCE_REQUIREMENTS  0x0B
820 #define	IRP_MN_QUERY_DEVICE_TEXT            0x0C
821 #define	IRP_MN_FILTER_RESOURCE_REQUIREMENTS 0x0D
822 
823 #define	IRP_MN_READ_CONFIG                  0x0F
824 #define	IRP_MN_WRITE_CONFIG                 0x10
825 #define	IRP_MN_EJECT                        0x11
826 #define	IRP_MN_SET_LOCK                     0x12
827 #define	IRP_MN_QUERY_ID                     0x13
828 #define	IRP_MN_QUERY_PNP_DEVICE_STATE       0x14
829 #define	IRP_MN_QUERY_BUS_INFORMATION        0x15
830 #define	IRP_MN_DEVICE_USAGE_NOTIFICATION    0x16
831 #define	IRP_MN_SURPRISE_REMOVAL             0x17
832 #define	IRP_MN_QUERY_LEGACY_BUS_INFORMATION 0x18
833 
834 #define	IRP_MN_WAIT_WAKE                    0x00
835 #define	IRP_MN_POWER_SEQUENCE               0x01
836 #define	IRP_MN_SET_POWER                    0x02
837 #define	IRP_MN_QUERY_POWER                  0x03
838 
839 #define	IRP_MN_QUERY_ALL_DATA               0x00
840 #define	IRP_MN_QUERY_SINGLE_INSTANCE        0x01
841 #define	IRP_MN_CHANGE_SINGLE_INSTANCE       0x02
842 #define	IRP_MN_CHANGE_SINGLE_ITEM           0x03
843 #define	IRP_MN_ENABLE_EVENTS                0x04
844 #define	IRP_MN_DISABLE_EVENTS               0x05
845 #define	IRP_MN_ENABLE_COLLECTION            0x06
846 #define	IRP_MN_DISABLE_COLLECTION           0x07
847 #define	IRP_MN_REGINFO                      0x08
848 #define	IRP_MN_EXECUTE_METHOD               0x09
849 #define	IRP_MN_REGINFO_EX                   0x0b
850 
851 /* IRP flags */
852 
853 #define	IRP_NOCACHE                     0x00000001
854 #define	IRP_PAGING_IO                   0x00000002
855 #define	IRP_MOUNT_COMPLETION            0x00000002
856 #define	IRP_SYNCHRONOUS_API             0x00000004
857 #define	IRP_ASSOCIATED_IRP              0x00000008
858 #define	IRP_BUFFERED_IO                 0x00000010
859 #define	IRP_DEALLOCATE_BUFFER           0x00000020
860 #define	IRP_INPUT_OPERATION             0x00000040
861 #define	IRP_SYNCHRONOUS_PAGING_IO       0x00000040
862 #define	IRP_CREATE_OPERATION            0x00000080
863 #define	IRP_READ_OPERATION              0x00000100
864 #define	IRP_WRITE_OPERATION             0x00000200
865 #define	IRP_CLOSE_OPERATION             0x00000400
866 #define	IRP_DEFER_IO_COMPLETION         0x00000800
867 #define	IRP_OB_QUERY_NAME               0x00001000
868 #define	IRP_HOLD_DEVICE_QUEUE           0x00002000
869 #define	IRP_RETRY_IO_COMPLETION         0x00004000
870 #define	IRP_CLASS_CACHE_OPERATION       0x00008000
871 #define	IRP_SET_USER_EVENT              IRP_CLOSE_OPERATION
872 
873 /* IRP I/O control flags */
874 
875 #define	IRP_QUOTA_CHARGED               0x01
876 #define	IRP_ALLOCATED_MUST_SUCCEED      0x02
877 #define	IRP_ALLOCATED_FIXED_SIZE        0x04
878 #define	IRP_LOOKASIDE_ALLOCATION        0x08
879 
880 /* I/O method types */
881 
882 #define	METHOD_BUFFERED			0
883 #define	METHOD_IN_DIRECT		1
884 #define	METHOD_OUT_DIRECT		2
885 #define	METHOD_NEITHER			3
886 
887 /* File access types */
888 
889 #define	FILE_ANY_ACCESS			0x0000
890 #define	FILE_SPECIAL_ACCESS		FILE_ANY_ACCESS
891 #define	FILE_READ_ACCESS		0x0001
892 #define	FILE_WRITE_ACCESS		0x0002
893 
894 /* Recover I/O access method from IOCTL code. */
895 
896 #define	IO_METHOD(x)			((x) & 0xFFFFFFFC)
897 
898 /* Recover function code from IOCTL code */
899 
900 #define	IO_FUNC(x)			(((x) & 0x7FFC) >> 2)
901 
902 /* Macro to construct an IOCTL code. */
903 
904 #define	IOCTL_CODE(dev, func, iomethod, acc)	\
905 	((dev) << 16) | (acc << 14) | (func << 2) | (iomethod))
906 
907 struct io_status_block {
908 	union {
909 		uint32_t		isb_status;
910 		void			*isb_ptr;
911 	} u;
912 	register_t		isb_info;
913 };
914 #define	isb_status		u.isb_status
915 #define	isb_ptr			u.isb_ptr
916 
917 typedef struct io_status_block io_status_block;
918 
919 struct kapc {
920 	uint16_t		apc_type;
921 	uint16_t		apc_size;
922 	uint32_t		apc_spare0;
923 	void			*apc_thread;
924 	list_entry		apc_list;
925 	void			*apc_kernfunc;
926 	void			*apc_rundownfunc;
927 	void			*apc_normalfunc;
928 	void			*apc_normctx;
929 	void			*apc_sysarg1;
930 	void			*apc_sysarg2;
931 	uint8_t			apc_stateidx;
932 	uint8_t			apc_cpumode;
933 	uint8_t			apc_inserted;
934 };
935 
936 typedef struct kapc kapc;
937 
938 typedef uint32_t (*completion_func)(device_object *,
939 	struct irp *, void *);
940 typedef uint32_t (*cancel_func)(device_object *,
941 	struct irp *);
942 
943 struct io_stack_location {
944 	uint8_t			isl_major;
945 	uint8_t			isl_minor;
946 	uint8_t			isl_flags;
947 	uint8_t			isl_ctl;
948 
949 	/*
950 	 * There's a big-ass union here in the actual Windows
951 	 * definition of the structure, but it contains stuff
952 	 * that doesn't really apply to BSD, and defining it
953 	 * all properly would require duplicating over a dozen
954 	 * other structures that we'll never use. Since the
955 	 * io_stack_location structure is opaque to drivers
956 	 * anyway, I'm not going to bother with the extra crap.
957 	 */
958 
959 	union {
960 		struct {
961 			uint32_t		isl_len;
962 			uint32_t		*isl_key;
963 			uint64_t		isl_byteoff;
964 		} isl_read;
965 		struct {
966 			uint32_t		isl_len;
967 			uint32_t		*isl_key;
968 			uint64_t		isl_byteoff;
969 		} isl_write;
970 		struct {
971 			uint32_t		isl_obuflen;
972 			uint32_t		isl_ibuflen;
973 			uint32_t		isl_iocode;
974 			void			*isl_type3ibuf;
975 		} isl_ioctl;
976 		struct {
977 			void			*isl_arg1;
978 			void			*isl_arg2;
979 			void			*isl_arg3;
980 			void			*isl_arg4;
981 		} isl_others;
982 	} isl_parameters __attribute__((packed));
983 
984 	void			*isl_devobj;
985 	void			*isl_fileobj;
986 	completion_func		isl_completionfunc;
987 	void			*isl_completionctx;
988 };
989 
990 typedef struct io_stack_location io_stack_location;
991 
992 /* Stack location control flags */
993 
994 #define	SL_PENDING_RETURNED		0x01
995 #define	SL_INVOKE_ON_CANCEL		0x20
996 #define	SL_INVOKE_ON_SUCCESS		0x40
997 #define	SL_INVOKE_ON_ERROR		0x80
998 
999 struct irp {
1000 	uint16_t		irp_type;
1001 	uint16_t		irp_size;
1002 	mdl			*irp_mdl;
1003 	uint32_t		irp_flags;
1004 	union {
1005 		struct irp		*irp_master;
1006 		uint32_t		irp_irpcnt;
1007 		void			*irp_sysbuf;
1008 	} irp_assoc;
1009 	list_entry		irp_thlist;
1010 	io_status_block		irp_iostat;
1011 	uint8_t			irp_reqmode;
1012 	uint8_t			irp_pendingreturned;
1013 	uint8_t			irp_stackcnt;
1014 	uint8_t			irp_currentstackloc;
1015 	uint8_t			irp_cancel;
1016 	uint8_t			irp_cancelirql;
1017 	uint8_t			irp_apcenv;
1018 	uint8_t			irp_allocflags;
1019 	io_status_block		*irp_usriostat;
1020 	nt_kevent		*irp_usrevent;
1021 	union {
1022 		struct {
1023 			void			*irp_apcfunc;
1024 			void			*irp_apcctx;
1025 		} irp_asyncparms;
1026 		uint64_t			irp_allocsz;
1027 	} irp_overlay;
1028 	cancel_func		irp_cancelfunc;
1029 	void			*irp_userbuf;
1030 
1031 	/* Windows kernel info */
1032 
1033 	union {
1034 		struct {
1035 			union {
1036 				kdevice_qentry			irp_dqe;
1037 				struct {
1038 					void			*irp_drvctx[4];
1039 				} s1;
1040 			} u1;
1041 			void			*irp_thread;
1042 			char			*irp_auxbuf;
1043 			struct {
1044 				list_entry			irp_list;
1045 				union {
1046 					io_stack_location	*irp_csl;
1047 					uint32_t		irp_pkttype;
1048 				} u2;
1049 			} s2;
1050 			void			*irp_fileobj;
1051 		} irp_overlay;
1052 		union {
1053 			kapc			irp_apc;
1054 			struct {
1055 				void		*irp_ep;
1056 				void		*irp_dev;
1057 			} irp_usb;
1058 		} irp_misc;
1059 		void			*irp_compkey;
1060 	} irp_tail;
1061 };
1062 
1063 #define	irp_csl			s2.u2.irp_csl
1064 #define	irp_pkttype		s2.u2.irp_pkttype
1065 
1066 #define	IRP_NDIS_DEV(irp)	(irp)->irp_tail.irp_misc.irp_usb.irp_dev
1067 #define	IRP_NDISUSB_EP(irp)	(irp)->irp_tail.irp_misc.irp_usb.irp_ep
1068 
1069 typedef struct irp irp;
1070 
1071 #define	InterlockedExchangePointer(dst, val)				\
1072 	(void *)InterlockedExchange((uint32_t *)(dst), (uintptr_t)(val))
1073 
1074 #define	IoSizeOfIrp(ssize)						\
1075 	((uint16_t) (sizeof(irp) + ((ssize) * (sizeof(io_stack_location)))))
1076 
1077 #define	IoSetCancelRoutine(irp, func)					\
1078 	(cancel_func)InterlockedExchangePointer(			\
1079 	(void *)&(ip)->irp_cancelfunc, (void *)(func))
1080 
1081 #define	IoSetCancelValue(irp, val)					\
1082 	(u_long)InterlockedExchangePointer(				\
1083 	(void *)&(ip)->irp_cancel, (void *)(val))
1084 
1085 #define	IoGetCurrentIrpStackLocation(irp)				\
1086 	(irp)->irp_tail.irp_overlay.irp_csl
1087 
1088 #define	IoGetNextIrpStackLocation(irp)					\
1089 	((irp)->irp_tail.irp_overlay.irp_csl - 1)
1090 
1091 #define	IoSetNextIrpStackLocation(irp)					\
1092 	do {								\
1093 		irp->irp_currentstackloc--;				\
1094 		irp->irp_tail.irp_overlay.irp_csl--;			\
1095 	} while(0)
1096 
1097 #define	IoSetCompletionRoutine(irp, func, ctx, ok, err, cancel)		\
1098 	do {								\
1099 		io_stack_location		*s;			\
1100 		s = IoGetNextIrpStackLocation((irp));			\
1101 		s->isl_completionfunc = (func);				\
1102 		s->isl_completionctx = (ctx);				\
1103 		s->isl_ctl = 0;						\
1104 		if (ok) s->isl_ctl = SL_INVOKE_ON_SUCCESS;		\
1105 		if (err) s->isl_ctl |= SL_INVOKE_ON_ERROR;		\
1106 		if (cancel) s->isl_ctl |= SL_INVOKE_ON_CANCEL;		\
1107 	} while(0)
1108 
1109 #define	IoMarkIrpPending(irp)						\
1110 	IoGetCurrentIrpStackLocation(irp)->isl_ctl |= SL_PENDING_RETURNED
1111 #define	IoUnmarkIrpPending(irp)						\
1112 	IoGetCurrentIrpStackLocation(irp)->isl_ctl &= ~SL_PENDING_RETURNED
1113 
1114 #define	IoCopyCurrentIrpStackLocationToNext(irp)			\
1115 	do {								\
1116 		io_stack_location *src, *dst;				\
1117 		src = IoGetCurrentIrpStackLocation(irp);		\
1118 		dst = IoGetNextIrpStackLocation(irp);			\
1119 		bcopy((char *)src, (char *)dst,				\
1120 		    offsetof(io_stack_location, isl_completionfunc));	\
1121 	} while(0)
1122 
1123 #define	IoSkipCurrentIrpStackLocation(irp)				\
1124 	do {								\
1125 		(irp)->irp_currentstackloc++;				\
1126 		(irp)->irp_tail.irp_overlay.irp_csl++;			\
1127 	} while(0)
1128 
1129 #define	IoInitializeDpcRequest(dobj, dpcfunc)				\
1130 	KeInitializeDpc(&(dobj)->do_dpc, dpcfunc, dobj)
1131 
1132 #define	IoRequestDpc(dobj, irp, ctx)					\
1133 	KeInsertQueueDpc(&(dobj)->do_dpc, irp, ctx)
1134 
1135 typedef uint32_t (*driver_dispatch)(device_object *, irp *);
1136 
1137 /*
1138  * The driver_object is allocated once for each driver that's loaded
1139  * into the system. A new one is allocated for each driver and
1140  * populated a bit via the driver's DriverEntry function.
1141  * In general, a Windows DriverEntry() function will provide a pointer
1142  * to its AddDevice() method and set up the dispatch table.
1143  * For NDIS drivers, this is all done behind the scenes in the
1144  * NdisInitializeWrapper() and/or NdisMRegisterMiniport() routines.
1145  */
1146 
1147 struct driver_object {
1148 	uint16_t		dro_type;
1149 	uint16_t		dro_size;
1150 	device_object		*dro_devobj;
1151 	uint32_t		dro_flags;
1152 	void			*dro_driverstart;
1153 	uint32_t		dro_driversize;
1154 	void			*dro_driversection;
1155 	driver_extension	*dro_driverext;
1156 	unicode_string		dro_drivername;
1157 	unicode_string		*dro_hwdb;
1158 	void			*dro_pfastiodispatch;
1159 	void			*dro_driverinitfunc;
1160 	void			*dro_driverstartiofunc;
1161 	void			*dro_driverunloadfunc;
1162 	driver_dispatch		dro_dispatch[IRP_MJ_MAXIMUM_FUNCTION + 1];
1163 };
1164 
1165 typedef struct driver_object driver_object;
1166 
1167 #define	DEVPROP_DEVICE_DESCRIPTION	0x00000000
1168 #define	DEVPROP_HARDWARE_ID		0x00000001
1169 #define	DEVPROP_COMPATIBLE_IDS		0x00000002
1170 #define	DEVPROP_BOOTCONF		0x00000003
1171 #define	DEVPROP_BOOTCONF_TRANSLATED	0x00000004
1172 #define	DEVPROP_CLASS_NAME		0x00000005
1173 #define	DEVPROP_CLASS_GUID		0x00000006
1174 #define	DEVPROP_DRIVER_KEYNAME		0x00000007
1175 #define	DEVPROP_MANUFACTURER		0x00000008
1176 #define	DEVPROP_FRIENDLYNAME		0x00000009
1177 #define	DEVPROP_LOCATION_INFO		0x0000000A
1178 #define	DEVPROP_PHYSDEV_NAME		0x0000000B
1179 #define	DEVPROP_BUSTYPE_GUID		0x0000000C
1180 #define	DEVPROP_LEGACY_BUSTYPE		0x0000000D
1181 #define	DEVPROP_BUS_NUMBER		0x0000000E
1182 #define	DEVPROP_ENUMERATOR_NAME		0x0000000F
1183 #define	DEVPROP_ADDRESS			0x00000010
1184 #define	DEVPROP_UINUMBER		0x00000011
1185 #define	DEVPROP_INSTALL_STATE		0x00000012
1186 #define	DEVPROP_REMOVAL_POLICY		0x00000013
1187 
1188 /* Various supported device types (used with IoCreateDevice()) */
1189 
1190 #define	FILE_DEVICE_BEEP		0x00000001
1191 #define	FILE_DEVICE_CD_ROM		0x00000002
1192 #define	FILE_DEVICE_CD_ROM_FILE_SYSTEM	0x00000003
1193 #define	FILE_DEVICE_CONTROLLER		0x00000004
1194 #define	FILE_DEVICE_DATALINK		0x00000005
1195 #define	FILE_DEVICE_DFS			0x00000006
1196 #define	FILE_DEVICE_DISK		0x00000007
1197 #define	FILE_DEVICE_DISK_FILE_SYSTEM	0x00000008
1198 #define	FILE_DEVICE_FILE_SYSTEM		0x00000009
1199 #define	FILE_DEVICE_INPORT_PORT		0x0000000A
1200 #define	FILE_DEVICE_KEYBOARD		0x0000000B
1201 #define	FILE_DEVICE_MAILSLOT		0x0000000C
1202 #define	FILE_DEVICE_MIDI_IN		0x0000000D
1203 #define	FILE_DEVICE_MIDI_OUT		0x0000000E
1204 #define	FILE_DEVICE_MOUSE		0x0000000F
1205 #define	FILE_DEVICE_MULTI_UNC_PROVIDER	0x00000010
1206 #define	FILE_DEVICE_NAMED_PIPE		0x00000011
1207 #define	FILE_DEVICE_NETWORK		0x00000012
1208 #define	FILE_DEVICE_NETWORK_BROWSER	0x00000013
1209 #define	FILE_DEVICE_NETWORK_FILE_SYSTEM	0x00000014
1210 #define	FILE_DEVICE_NULL		0x00000015
1211 #define	FILE_DEVICE_PARALLEL_PORT	0x00000016
1212 #define	FILE_DEVICE_PHYSICAL_NETCARD	0x00000017
1213 #define	FILE_DEVICE_PRINTER		0x00000018
1214 #define	FILE_DEVICE_SCANNER		0x00000019
1215 #define	FILE_DEVICE_SERIAL_MOUSE_PORT	0x0000001A
1216 #define	FILE_DEVICE_SERIAL_PORT		0x0000001B
1217 #define	FILE_DEVICE_SCREEN		0x0000001C
1218 #define	FILE_DEVICE_SOUND		0x0000001D
1219 #define	FILE_DEVICE_STREAMS		0x0000001E
1220 #define	FILE_DEVICE_TAPE		0x0000001F
1221 #define	FILE_DEVICE_TAPE_FILE_SYSTEM	0x00000020
1222 #define	FILE_DEVICE_TRANSPORT		0x00000021
1223 #define	FILE_DEVICE_UNKNOWN		0x00000022
1224 #define	FILE_DEVICE_VIDEO		0x00000023
1225 #define	FILE_DEVICE_VIRTUAL_DISK	0x00000024
1226 #define	FILE_DEVICE_WAVE_IN		0x00000025
1227 #define	FILE_DEVICE_WAVE_OUT		0x00000026
1228 #define	FILE_DEVICE_8042_PORT		0x00000027
1229 #define	FILE_DEVICE_NETWORK_REDIRECTOR	0x00000028
1230 #define	FILE_DEVICE_BATTERY		0x00000029
1231 #define	FILE_DEVICE_BUS_EXTENDER	0x0000002A
1232 #define	FILE_DEVICE_MODEM		0x0000002B
1233 #define	FILE_DEVICE_VDM			0x0000002C
1234 #define	FILE_DEVICE_MASS_STORAGE	0x0000002D
1235 #define	FILE_DEVICE_SMB			0x0000002E
1236 #define	FILE_DEVICE_KS			0x0000002F
1237 #define	FILE_DEVICE_CHANGER		0x00000030
1238 #define	FILE_DEVICE_SMARTCARD		0x00000031
1239 #define	FILE_DEVICE_ACPI		0x00000032
1240 #define	FILE_DEVICE_DVD			0x00000033
1241 #define	FILE_DEVICE_FULLSCREEN_VIDEO	0x00000034
1242 #define	FILE_DEVICE_DFS_FILE_SYSTEM	0x00000035
1243 #define	FILE_DEVICE_DFS_VOLUME		0x00000036
1244 #define	FILE_DEVICE_SERENUM		0x00000037
1245 #define	FILE_DEVICE_TERMSRV		0x00000038
1246 #define	FILE_DEVICE_KSEC		0x00000039
1247 #define	FILE_DEVICE_FIPS		0x0000003A
1248 
1249 /* Device characteristics */
1250 
1251 #define	FILE_REMOVABLE_MEDIA		0x00000001
1252 #define	FILE_READ_ONLY_DEVICE		0x00000002
1253 #define	FILE_FLOPPY_DISKETTE		0x00000004
1254 #define	FILE_WRITE_ONCE_MEDIA		0x00000008
1255 #define	FILE_REMOTE_DEVICE		0x00000010
1256 #define	FILE_DEVICE_IS_MOUNTED		0x00000020
1257 #define	FILE_VIRTUAL_VOLUME		0x00000040
1258 #define	FILE_AUTOGENERATED_DEVICE_NAME	0x00000080
1259 #define	FILE_DEVICE_SECURE_OPEN		0x00000100
1260 
1261 /* Status codes */
1262 
1263 #define	STATUS_SUCCESS			0x00000000
1264 #define	STATUS_USER_APC			0x000000C0
1265 #define	STATUS_KERNEL_APC		0x00000100
1266 #define	STATUS_ALERTED			0x00000101
1267 #define	STATUS_TIMEOUT			0x00000102
1268 #define	STATUS_PENDING			0x00000103
1269 #define	STATUS_FAILURE			0xC0000001
1270 #define	STATUS_NOT_IMPLEMENTED		0xC0000002
1271 #define	STATUS_ACCESS_VIOLATION		0xC0000005
1272 #define	STATUS_INVALID_PARAMETER	0xC000000D
1273 #define	STATUS_INVALID_DEVICE_REQUEST	0xC0000010
1274 #define	STATUS_MORE_PROCESSING_REQUIRED	0xC0000016
1275 #define	STATUS_NO_MEMORY		0xC0000017
1276 #define	STATUS_BUFFER_TOO_SMALL		0xC0000023
1277 #define	STATUS_MUTANT_NOT_OWNED		0xC0000046
1278 #define	STATUS_NOT_SUPPORTED		0xC00000BB
1279 #define	STATUS_INVALID_PARAMETER_2	0xC00000F0
1280 #define	STATUS_INSUFFICIENT_RESOURCES	0xC000009A
1281 #define	STATUS_DEVICE_NOT_CONNECTED	0xC000009D
1282 #define	STATUS_CANCELLED		0xC0000120
1283 #define	STATUS_NOT_FOUND		0xC0000225
1284 #define	STATUS_DEVICE_REMOVED		0xC00002B6
1285 
1286 #define	STATUS_WAIT_0			0x00000000
1287 
1288 /* Memory pool types, for ExAllocatePoolWithTag() */
1289 
1290 #define	NonPagedPool			0x00000000
1291 #define	PagedPool			0x00000001
1292 #define	NonPagedPoolMustSucceed		0x00000002
1293 #define	DontUseThisType			0x00000003
1294 #define	NonPagedPoolCacheAligned	0x00000004
1295 #define	PagedPoolCacheAligned		0x00000005
1296 #define	NonPagedPoolCacheAlignedMustS	0x00000006
1297 #define	MaxPoolType			0x00000007
1298 
1299 /*
1300  * IO_WORKITEM is an opaque structures that must be allocated
1301  * via IoAllocateWorkItem() and released via IoFreeWorkItem().
1302  * Consequently, we can define it any way we want.
1303  */
1304 typedef void (*io_workitem_func)(device_object *, void *);
1305 
1306 struct io_workitem {
1307 	io_workitem_func	iw_func;
1308 	void			*iw_ctx;
1309 	list_entry		iw_listentry;
1310 	device_object		*iw_dobj;
1311 	int			iw_idx;
1312 };
1313 
1314 typedef struct io_workitem io_workitem;
1315 
1316 #define	WORKQUEUE_CRITICAL	0
1317 #define	WORKQUEUE_DELAYED	1
1318 #define	WORKQUEUE_HYPERCRITICAL	2
1319 
1320 #define	WORKITEM_THREADS	4
1321 #define	WORKITEM_LEGACY_THREAD	3
1322 #define	WORKIDX_INC(x)		(x) = (x + 1) % WORKITEM_LEGACY_THREAD
1323 
1324 /*
1325  * Older, deprecated work item API, needed to support NdisQueueWorkItem().
1326  */
1327 
1328 struct work_queue_item;
1329 
1330 typedef void (*work_item_func)(struct work_queue_item *, void *);
1331 
1332 struct work_queue_item {
1333 	list_entry		wqi_entry;
1334 	work_item_func		wqi_func;
1335 	void			*wqi_ctx;
1336 };
1337 
1338 typedef struct work_queue_item work_queue_item;
1339 
1340 #define	ExInitializeWorkItem(w, func, ctx)		\
1341 	do {						\
1342 		(w)->wqi_func = (func);			\
1343 		(w)->wqi_ctx = (ctx);			\
1344 		InitializeListHead(&((w)->wqi_entry));	\
1345 	} while (0)
1346 
1347 /*
1348  * FreeBSD's kernel stack is 2 pages in size by default. The
1349  * Windows stack is larger, so we need to give our threads more
1350  * stack pages. 4 should be enough, we use 8 just to extra safe.
1351  */
1352 #define	NDIS_KSTACK_PAGES	8
1353 
1354 /*
1355  * Different kinds of function wrapping we can do.
1356  */
1357 
1358 #define	WINDRV_WRAP_STDCALL	1
1359 #define	WINDRV_WRAP_FASTCALL	2
1360 #define	WINDRV_WRAP_REGPARM	3
1361 #define	WINDRV_WRAP_CDECL	4
1362 #define	WINDRV_WRAP_AMD64	5
1363 
1364 struct drvdb_ent {
1365 	driver_object		*windrv_object;
1366 	void			*windrv_devlist;
1367 	ndis_cfg		*windrv_regvals;
1368 	interface_type		windrv_bustype;
1369 	STAILQ_ENTRY(drvdb_ent) link;
1370 };
1371 
1372 extern image_patch_table ntoskrnl_functbl[];
1373 #ifdef __amd64__
1374 extern struct kuser_shared_data kuser_shared_data;
1375 #endif
1376 typedef void (*funcptr)(void);
1377 typedef int (*matchfuncptr)(interface_type, void *, void *);
1378 
1379 __BEGIN_DECLS
1380 extern int windrv_libinit(void);
1381 extern int windrv_libfini(void);
1382 extern driver_object *windrv_lookup(vm_offset_t, char *);
1383 extern struct drvdb_ent *windrv_match(matchfuncptr, void *);
1384 extern int windrv_load(module_t, vm_offset_t, int, interface_type,
1385 	void *, ndis_cfg *);
1386 extern int windrv_unload(module_t, vm_offset_t, int);
1387 extern int windrv_create_pdo(driver_object *, device_t);
1388 extern void windrv_destroy_pdo(driver_object *, device_t);
1389 extern device_object *windrv_find_pdo(driver_object *, device_t);
1390 extern int windrv_bus_attach(driver_object *, char *);
1391 extern int windrv_wrap(funcptr, funcptr *, int, int);
1392 extern int windrv_unwrap(funcptr);
1393 extern void ctxsw_utow(void);
1394 extern void ctxsw_wtou(void);
1395 
1396 extern int ntoskrnl_libinit(void);
1397 extern int ntoskrnl_libfini(void);
1398 
1399 extern void ntoskrnl_intr(void *);
1400 extern void ntoskrnl_time(uint64_t *);
1401 
1402 extern uint16_t ExQueryDepthSList(slist_header *);
1403 extern slist_entry
1404 	*InterlockedPushEntrySList(slist_header *, slist_entry *);
1405 extern slist_entry *InterlockedPopEntrySList(slist_header *);
1406 extern uint32_t RtlUnicodeStringToAnsiString(ansi_string *,
1407 	unicode_string *, uint8_t);
1408 extern uint32_t RtlAnsiStringToUnicodeString(unicode_string *,
1409 	ansi_string *, uint8_t);
1410 extern void RtlInitAnsiString(ansi_string *, char *);
1411 extern void RtlInitUnicodeString(unicode_string *,
1412 	uint16_t *);
1413 extern void RtlFreeUnicodeString(unicode_string *);
1414 extern void RtlFreeAnsiString(ansi_string *);
1415 extern void KeInitializeDpc(kdpc *, void *, void *);
1416 extern uint8_t KeInsertQueueDpc(kdpc *, void *, void *);
1417 extern uint8_t KeRemoveQueueDpc(kdpc *);
1418 extern void KeSetImportanceDpc(kdpc *, uint32_t);
1419 extern void KeSetTargetProcessorDpc(kdpc *, uint8_t);
1420 extern void KeFlushQueuedDpcs(void);
1421 extern uint32_t KeGetCurrentProcessorNumber(void);
1422 extern void KeInitializeTimer(ktimer *);
1423 extern void KeInitializeTimerEx(ktimer *, uint32_t);
1424 extern uint8_t KeSetTimer(ktimer *, int64_t, kdpc *);
1425 extern uint8_t KeSetTimerEx(ktimer *, int64_t, uint32_t, kdpc *);
1426 extern uint8_t KeCancelTimer(ktimer *);
1427 extern uint8_t KeReadStateTimer(ktimer *);
1428 extern uint32_t KeWaitForSingleObject(void *, uint32_t,
1429 	uint32_t, uint8_t, int64_t *);
1430 extern void KeInitializeEvent(nt_kevent *, uint32_t, uint8_t);
1431 extern void KeClearEvent(nt_kevent *);
1432 extern uint32_t KeReadStateEvent(nt_kevent *);
1433 extern uint32_t KeSetEvent(nt_kevent *, uint32_t, uint8_t);
1434 extern uint32_t KeResetEvent(nt_kevent *);
1435 #ifdef __i386__
1436 extern void KefAcquireSpinLockAtDpcLevel(kspin_lock *);
1437 extern void KefReleaseSpinLockFromDpcLevel(kspin_lock *);
1438 extern uint8_t KeAcquireSpinLockRaiseToDpc(kspin_lock *);
1439 #else
1440 extern void KeAcquireSpinLockAtDpcLevel(kspin_lock *);
1441 extern void KeReleaseSpinLockFromDpcLevel(kspin_lock *);
1442 #endif
1443 extern void KeInitializeSpinLock(kspin_lock *);
1444 extern uint8_t KeAcquireInterruptSpinLock(kinterrupt *);
1445 extern void KeReleaseInterruptSpinLock(kinterrupt *, uint8_t);
1446 extern uint8_t KeSynchronizeExecution(kinterrupt *, void *, void *);
1447 extern uintptr_t InterlockedExchange(volatile uint32_t *,
1448 	uintptr_t);
1449 extern void *ExAllocatePoolWithTag(uint32_t, size_t, uint32_t);
1450 extern void ExFreePool(void *);
1451 extern uint32_t IoConnectInterrupt(kinterrupt **, void *, void *,
1452 	kspin_lock *, uint32_t, uint8_t, uint8_t, uint8_t, uint8_t,
1453 	uint32_t, uint8_t);
1454 extern uint8_t MmIsAddressValid(void *);
1455 extern void *MmGetSystemRoutineAddress(unicode_string *);
1456 extern void *MmMapIoSpace(uint64_t, uint32_t, uint32_t);
1457 extern void MmUnmapIoSpace(void *, size_t);
1458 extern void MmBuildMdlForNonPagedPool(mdl *);
1459 extern void IoDisconnectInterrupt(kinterrupt *);
1460 extern uint32_t IoAllocateDriverObjectExtension(driver_object *,
1461 	void *, uint32_t, void **);
1462 extern void *IoGetDriverObjectExtension(driver_object *, void *);
1463 extern uint32_t IoCreateDevice(driver_object *, uint32_t,
1464 	unicode_string *, uint32_t, uint32_t, uint8_t, device_object **);
1465 extern void IoDeleteDevice(device_object *);
1466 extern device_object *IoGetAttachedDevice(device_object *);
1467 extern uint32_t IofCallDriver(device_object *, irp *);
1468 extern void IofCompleteRequest(irp *, uint8_t);
1469 extern void IoAcquireCancelSpinLock(uint8_t *);
1470 extern void IoReleaseCancelSpinLock(uint8_t);
1471 extern uint8_t IoCancelIrp(irp *);
1472 extern void IoDetachDevice(device_object *);
1473 extern device_object *IoAttachDeviceToDeviceStack(device_object *,
1474 	device_object *);
1475 extern mdl *IoAllocateMdl(void *, uint32_t, uint8_t, uint8_t, irp *);
1476 extern void IoFreeMdl(mdl *);
1477 extern io_workitem *IoAllocateWorkItem(device_object *);
1478 extern void ExQueueWorkItem(work_queue_item *, u_int32_t);
1479 extern void IoFreeWorkItem(io_workitem *);
1480 extern void IoQueueWorkItem(io_workitem *, io_workitem_func,
1481 	uint32_t, void *);
1482 
1483 #define	IoCallDriver(a, b)		IofCallDriver(a, b)
1484 #define	IoCompleteRequest(a, b)		IofCompleteRequest(a, b)
1485 
1486 /*
1487  * On the Windows x86 arch, KeAcquireSpinLock() and KeReleaseSpinLock()
1488  * routines live in the HAL. We try to imitate this behavior.
1489  */
1490 #ifdef __i386__
1491 #define	KI_USER_SHARED_DATA 0xffdf0000
1492 #define	KeAcquireSpinLock(a, b)	*(b) = KfAcquireSpinLock(a)
1493 #define	KeReleaseSpinLock(a, b)	KfReleaseSpinLock(a, b)
1494 #define	KeRaiseIrql(a, b)	*(b) = KfRaiseIrql(a)
1495 #define	KeLowerIrql(a)		KfLowerIrql(a)
1496 #define	KeAcquireSpinLockAtDpcLevel(a)	KefAcquireSpinLockAtDpcLevel(a)
1497 #define	KeReleaseSpinLockFromDpcLevel(a)  KefReleaseSpinLockFromDpcLevel(a)
1498 #endif /* __i386__ */
1499 
1500 #ifdef __amd64__
1501 #define	KI_USER_SHARED_DATA 0xfffff78000000000UL
1502 #define	KeAcquireSpinLock(a, b)	*(b) = KfAcquireSpinLock(a)
1503 #define	KeReleaseSpinLock(a, b)	KfReleaseSpinLock(a, b)
1504 
1505 /*
1506  * These may need to be redefined later;
1507  * not sure where they live on amd64 yet.
1508  */
1509 #define	KeRaiseIrql(a, b)	*(b) = KfRaiseIrql(a)
1510 #define	KeLowerIrql(a)		KfLowerIrql(a)
1511 #endif /* __amd64__ */
1512 
1513 __END_DECLS
1514 
1515 #endif /* _NTOSKRNL_VAR_H_ */
1516