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