Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[fs/lustre-release.git] / libcfs / include / libcfs / darwin / libcfs.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 #ifndef __LIBCFS_DARWIN_LIBCFS_H__
36 #define __LIBCFS_DARWIN_LIBCFS_H__
37
38 #ifndef __LIBCFS_LIBCFS_H__
39 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
40 #endif
41
42 #include <mach/mach_types.h>
43 #include <sys/errno.h>
44 #include <string.h>
45 #include <libcfs/darwin/darwin-types.h>
46 #include <libcfs/darwin/darwin-time.h>
47 #include <libcfs/darwin/darwin-prim.h>
48 #include <libcfs/darwin/darwin-mem.h>
49 #include <libcfs/darwin/darwin-lock.h>
50 #include <libcfs/darwin/darwin-fs.h>
51 #include <libcfs/darwin/darwin-tcpip.h>
52 #include <libcfs/darwin/kp30.h>
53
54 #ifdef __KERNEL__
55 # include <sys/types.h>
56 # include <sys/time.h>
57 # define do_gettimeofday(tv) microuptime(tv)
58 #else
59 # include <sys/time.h>
60 # define do_gettimeofday(tv) gettimeofday(tv, NULL);
61 typedef unsigned long long cycles_t;
62 #endif
63
64 #define __cpu_to_le64(x)                        OSSwapHostToLittleInt64(x)
65 #define __cpu_to_le32(x)                        OSSwapHostToLittleInt32(x)
66 #define __cpu_to_le16(x)                        OSSwapHostToLittleInt16(x)
67
68 #define __le16_to_cpu(x)                        OSSwapLittleToHostInt16(x)
69 #define __le32_to_cpu(x)                        OSSwapLittleToHostInt32(x)
70 #define __le64_to_cpu(x)                        OSSwapLittleToHostInt64(x)
71
72 #define cpu_to_le64(x)                          __cpu_to_le64(x)
73 #define cpu_to_le32(x)                          __cpu_to_le32(x)
74 #define cpu_to_le16(x)                          __cpu_to_le16(x)
75
76 #define le64_to_cpu(x)                          __le64_to_cpu(x)
77 #define le32_to_cpu(x)                          __le32_to_cpu(x)
78 #define le16_to_cpu(x)                          __le16_to_cpu(x)
79
80 #define __swab16(x)                             OSSwapInt16(x)
81 #define __swab32(x)                             OSSwapInt32(x)
82 #define __swab64(x)                             OSSwapInt64(x)
83 #define __swab16s(x)                            do { *(x) = __swab16(*(x)); } while (0)
84 #define __swab32s(x)                            do { *(x) = __swab32(*(x)); } while (0)
85 #define __swab64s(x)                            do { *(x) = __swab64(*(x)); } while (0)
86
87
88 #ifdef __KERNEL__
89 # include <sys/systm.h>
90 # include <pexpert/pexpert.h>
91 /* Fix me */
92 # define THREAD_SIZE 8192
93 #else
94 # define THREAD_SIZE 8192
95 #endif
96 #define LUSTRE_TRACE_SIZE (THREAD_SIZE >> 5)
97
98 #define CHECK_STACK(msgdata, mask, cdls) do {} while(0)
99 #define CDEBUG_STACK() (0L)
100
101 /* Darwin has defined RETURN, so we have to undef it in lustre */
102 #ifdef RETURN
103 #undef RETURN
104 #endif
105
106 /*
107  * When this is enabled debugging messages are indented according to the
108  * current "nesting level". Nesting level in increased when ENTRY macro
109  * is executed, and decreased on EXIT and RETURN.
110  */
111 #ifdef __KERNEL__
112 #define ENTRY_NESTING_SUPPORT (0)
113 #endif
114
115 #if ENTRY_NESTING_SUPPORT
116
117 /*
118  * Currently ENTRY_NESTING_SUPPORT is only supported for XNU port. Basic
119  * idea is to keep per-thread pointer to small data structure (struct
120  * cfs_debug_data) describing current nesting level. In XNU unused
121  * proc->p_wmegs field in hijacked for this. On Linux
122  * current->journal_info can be used. In user space
123  * pthread_{g,s}etspecific().
124  *
125  * ENTRY macro allocates new cfs_debug_data on stack, and installs it as
126  * a current nesting level, storing old data in cfs_debug_data it just
127  * created.
128  *
129  * EXIT pops old value back.
130  *
131  */
132
133 /*
134  * One problem with this approach is that there is a lot of code that
135  * does ENTRY and then escapes scope without doing EXIT/RETURN. In this
136  * case per-thread current nesting level pointer is dangling (it points
137  * to the stack area that is possible already overridden). To detect
138  * such cases, we add two magic fields to the cfs_debug_data and check
139  * them whenever current nesting level pointer is dereferenced. While
140  * looking flaky this works because stack is always consumed
141  * "continously".
142  */
143 enum {
144         CDD_MAGIC1 = 0x02128506,
145         CDD_MAGIC2 = 0x42424242
146 };
147
148 struct cfs_debug_data {
149         unsigned int           magic1;
150         struct cfs_debug_data *parent;
151         int                    nesting_level;
152         unsigned int           magic2;
153 };
154
155 void __entry_nesting(struct cfs_debug_data *child);
156 void __exit_nesting(struct cfs_debug_data *child);
157 unsigned int __current_nesting_level(void);
158
159 #define ENTRY_NESTING                                           \
160 struct cfs_debug_data __cdd = { .magic1        = CDD_MAGIC1,    \
161                                 .parent        = NULL,          \
162                                 .nesting_level = 0,             \
163                                 .magic2        = CDD_MAGIC2 };  \
164 __entry_nesting(&__cdd);
165
166 #define EXIT_NESTING __exit_nesting(&__cdd)
167
168 /* ENTRY_NESTING_SUPPORT */
169 #else
170
171 #define ENTRY_NESTING   do {;} while (0)
172 #define EXIT_NESTING   do {;} while (0)
173 #define __current_nesting_level() (0)
174
175 /* ENTRY_NESTING_SUPPORT */
176 #endif
177
178 #define LUSTRE_LNET_PID          12345
179
180 #define _XNU_LIBCFS_H
181
182 /*
183  * Platform specific declarations for cfs_curproc API (libcfs/curproc.h)
184  *
185  * Implementation is in darwin-curproc.c
186  */
187 #define CFS_CURPROC_COMM_MAX    MAXCOMLEN
188 /*
189  * XNU has no capabilities
190  */
191 typedef __u32 cfs_kernel_cap_t;
192
193 #ifdef __KERNEL__
194 enum {
195         /* if you change this, update darwin-util.c:cfs_stack_trace_fill() */
196         CFS_STACK_TRACE_DEPTH = 16
197 };
198
199 struct cfs_stack_trace {
200         void *frame[CFS_STACK_TRACE_DEPTH];
201 };
202
203 #define printk(format, args...)                 printf(format, ## args)
204
205 #ifdef WITH_WATCHDOG
206 #undef WITH_WATCHDOG
207 #endif
208
209 #endif /* __KERNEL__ */
210
211 #endif /* _XNU_LIBCFS_H */