Whamcloud - gitweb
eb4d8f35982bb076c246eca81fb6ad0c5cd5de8f
[fs/lustre-release.git] / libcfs / include / libcfs / darwin / libcfs.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #ifndef __LIBCFS_DARWIN_LIBCFS_H__
5 #define __LIBCFS_DARWIN_LIBCFS_H__
6
7 #ifndef __LIBCFS_LIBCFS_H__
8 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
9 #endif
10
11 #include <mach/mach_types.h>
12 #include <sys/errno.h>
13 #include <string.h>
14 #include <libcfs/darwin/darwin-types.h>
15 #include <libcfs/darwin/darwin-time.h>
16 #include <libcfs/darwin/darwin-prim.h>
17 #include <libcfs/darwin/darwin-mem.h>
18 #include <libcfs/darwin/darwin-lock.h>
19 #include <libcfs/darwin/darwin-fs.h>
20 #include <libcfs/darwin/darwin-tcpip.h>
21
22 #ifdef __KERNEL__
23 # include <sys/types.h>
24 # include <sys/time.h>
25 # define do_gettimeofday(tv) microuptime(tv)
26 #else
27 # include <sys/time.h>
28 # define do_gettimeofday(tv) gettimeofday(tv, NULL);
29 typedef unsigned long long cycles_t;
30 #endif
31
32 #define __cpu_to_le64(x)                        OSSwapHostToLittleInt64(x)
33 #define __cpu_to_le32(x)                        OSSwapHostToLittleInt32(x)
34 #define __cpu_to_le16(x)                        OSSwapHostToLittleInt16(x)
35
36 #define __le16_to_cpu(x)                        OSSwapLittleToHostInt16(x)
37 #define __le32_to_cpu(x)                        OSSwapLittleToHostInt32(x)
38 #define __le64_to_cpu(x)                        OSSwapLittleToHostInt64(x)
39
40 #define cpu_to_le64(x)                          __cpu_to_le64(x)
41 #define cpu_to_le32(x)                          __cpu_to_le32(x)
42 #define cpu_to_le16(x)                          __cpu_to_le16(x)
43
44 #define le64_to_cpu(x)                          __le64_to_cpu(x)
45 #define le32_to_cpu(x)                          __le32_to_cpu(x)
46 #define le16_to_cpu(x)                          __le16_to_cpu(x)
47
48 #define __swab16(x)                             OSSwapInt16(x)
49 #define __swab32(x)                             OSSwapInt32(x)
50 #define __swab64(x)                             OSSwapInt64(x)
51 #define __swab16s(x)                            do { *(x) = __swab16(*(x)); } while (0)
52 #define __swab32s(x)                            do { *(x) = __swab32(*(x)); } while (0)
53 #define __swab64s(x)                            do { *(x) = __swab64(*(x)); } while (0)
54
55 struct ptldebug_header {
56         __u32 ph_len;
57         __u32 ph_flags;
58         __u32 ph_subsys;
59         __u32 ph_mask;
60         __u32 ph_cpu_id;
61         __u32 ph_sec;
62         __u64 ph_usec;
63         __u32 ph_stack;
64         __u32 ph_pid;
65         __u32 ph_extern_pid;
66         __u32 ph_line_num;
67 } __attribute__((packed));
68
69
70 #ifdef __KERNEL__
71 # include <sys/systm.h>
72 # include <pexpert/pexpert.h>
73 /* Fix me */
74 # define THREAD_SIZE 8192
75 #else
76 # define THREAD_SIZE 8192
77 #endif
78 #define LUSTRE_TRACE_SIZE (THREAD_SIZE >> 5)
79
80 #define CHECK_STACK() do { } while(0)
81 #define CDEBUG_STACK() (0L)
82
83 /* Darwin has defined RETURN, so we have to undef it in lustre */
84 #ifdef RETURN
85 #undef RETURN
86 #endif
87
88 /*
89  * When this is enabled debugging messages are indented according to the
90  * current "nesting level". Nesting level in increased when ENTRY macro
91  * is executed, and decreased on EXIT and RETURN.
92  */
93 #ifdef __KERNEL__
94 #define ENTRY_NESTING_SUPPORT (0)
95 #endif
96
97 #if ENTRY_NESTING_SUPPORT
98
99 /*
100  * Currently ENTRY_NESTING_SUPPORT is only supported for XNU port. Basic
101  * idea is to keep per-thread pointer to small data structure (struct
102  * cfs_debug_data) describing current nesting level. In XNU unused
103  * proc->p_wmegs field in hijacked for this. On Linux
104  * current->journal_info can be used. In user space
105  * pthread_{g,s}etspecific().
106  *
107  * ENTRY macro allocates new cfs_debug_data on stack, and installs it as
108  * a current nesting level, storing old data in cfs_debug_data it just
109  * created.
110  *
111  * EXIT pops old value back.
112  *
113  */
114
115 /*
116  * One problem with this approach is that there is a lot of code that
117  * does ENTRY and then escapes scope without doing EXIT/RETURN. In this
118  * case per-thread current nesting level pointer is dangling (it points
119  * to the stack area that is possible already overridden). To detect
120  * such cases, we add two magic fields to the cfs_debug_data and check
121  * them whenever current nesting level pointer is dereferenced. While
122  * looking flaky this works because stack is always consumed
123  * "continously".
124  */
125 enum {
126         CDD_MAGIC1 = 0x02128506,
127         CDD_MAGIC2 = 0x42424242
128 };
129
130 struct cfs_debug_data {
131         unsigned int           magic1;
132         struct cfs_debug_data *parent;
133         int                    nesting_level;
134         unsigned int           magic2;
135 };
136
137 void __entry_nesting(struct cfs_debug_data *child);
138 void __exit_nesting(struct cfs_debug_data *child);
139 unsigned int __current_nesting_level(void);
140
141 #define ENTRY_NESTING                                           \
142 struct cfs_debug_data __cdd = { .magic1        = CDD_MAGIC1,    \
143                                 .parent        = NULL,          \
144                                 .nesting_level = 0,             \
145                                 .magic2        = CDD_MAGIC2 };  \
146 __entry_nesting(&__cdd);
147
148 #define EXIT_NESTING __exit_nesting(&__cdd)
149
150 /* ENTRY_NESTING_SUPPORT */
151 #else
152
153 #define ENTRY_NESTING   do {;} while (0)
154 #define EXIT_NESTING   do {;} while (0)
155 #define __current_nesting_level() (0)
156
157 /* ENTRY_NESTING_SUPPORT */
158 #endif
159
160 #define LUSTRE_LNET_PID          12345
161
162 #define _XNU_LIBCFS_H
163
164 /*
165  * Platform specific declarations for cfs_curproc API (libcfs/curproc.h)
166  *
167  * Implementation is in darwin-curproc.c
168  */
169 #define CFS_CURPROC_COMM_MAX    MAXCOMLEN
170 /*
171  * XNU has no capabilities
172  */
173 typedef int cfs_kernel_cap_t;
174
175 #ifdef __KERNEL__
176 enum {
177         /* if you change this, update darwin-util.c:cfs_stack_trace_fill() */
178         CFS_STACK_TRACE_DEPTH = 16
179 };
180
181 struct cfs_stack_trace {
182         void *frame[CFS_STACK_TRACE_DEPTH];
183 };
184
185 #define printk(format, args...)                 printf(format, ## args)
186
187 #ifdef WITH_WATCHDOG
188 #undef WITH_WATCHDOG
189 #endif
190
191 #endif /* __KERNEL__ */
192
193 #endif /* _XNU_LIBCFS_H */