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