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