Whamcloud - gitweb
LU-1346 libcfs: cleanup macros in portals_compat25.h
[fs/lustre-release.git] / libcfs / include / libcfs / darwin / darwin-prim.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_CFS_PRIM_H__
36 #define __LIBCFS_DARWIN_CFS_PRIM_H__
37
38 #ifndef __LIBCFS_LIBCFS_H__
39 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
40 #endif
41
42 #ifdef __KERNEL__
43 #include <sys/types.h>
44 #include <sys/systm.h>
45
46 #ifndef __DARWIN8__
47 # ifndef __APPLE_API_PRIVATE
48 #  define __APPLE_API_PRIVATE
49 #  include <sys/user.h>
50 #  undef __APPLE_API_PRIVATE
51 # else
52 #  include <sys/user.h>
53 # endif
54 # include <mach/mach_traps.h>
55 # include <mach/thread_switch.h>
56 # include <machine/cpu_number.h>
57 #endif /* !__DARWIN8__ */
58
59 #include <sys/kernel.h>
60
61 #include <mach/thread_act.h>
62 #include <mach/mach_types.h>
63 #include <mach/time_value.h>
64 #include <kern/sched_prim.h>
65 #include <vm/pmap.h>
66 #include <vm/vm_kern.h>
67 #include <mach/machine/vm_param.h>
68 #include <machine/machine_routines.h>
69 #include <kern/clock.h>
70 #include <kern/thread_call.h>
71 #include <sys/param.h>
72 #include <sys/vm.h>
73
74 #include <libcfs/darwin/darwin-types.h>
75 #include <libcfs/darwin/darwin-utils.h>
76 #include <libcfs/darwin/darwin-lock.h>
77
78 /*
79  * Symbol functions for libcfs
80  *
81  * OSX has no facility for use to register symbol.
82  * So we have to implement it.
83  */
84 #define CFS_SYMBOL_LEN     64
85
86 struct  cfs_symbol {
87         char    name[CFS_SYMBOL_LEN];
88         void    *value;
89         int     ref;
90         struct  list_head sym_list;
91 };
92
93 extern kern_return_t            cfs_symbol_register(const char *, const void *);
94 extern kern_return_t            cfs_symbol_unregister(const char *);
95 extern void *                   cfs_symbol_get(const char *);
96 extern kern_return_t            cfs_symbol_put(const char *);
97
98 /*
99  * sysctl typedef
100  *
101  * User can register/unregister a list of sysctl_oids
102  * sysctl_oid is data struct of osx's sysctl-entry
103  */
104 #define         CONFIG_SYSCTL   1
105
106 typedef struct sysctl_oid *     cfs_sysctl_table_t;
107 typedef cfs_sysctl_table_t      cfs_sysctl_table_header_t;
108 cfs_sysctl_table_header_t       *cfs_register_sysctl_table (cfs_sysctl_table_t *table, int arg);
109 void cfs_unregister_sysctl_table (cfs_sysctl_table_header_t *table);
110
111 /*
112  * Proc file system APIs, no /proc fs support in OSX
113  */
114 typedef struct cfs_proc_dir_entry {
115         void            *data;
116 } cfs_proc_dir_entry_t;
117
118 cfs_proc_dir_entry_t * cfs_create_proc_entry(char *name, int mod,
119                                           cfs_proc_dir_entry_t *parent);
120 void cfs_free_proc_entry(cfs_proc_dir_entry_t *de);
121 void cfs_remove_proc_entry(char *name, cfs_proc_dir_entry_t *entry);
122
123 typedef int (cfs_read_proc_t)(char *page, char **start, off_t off,
124                           int count, int *eof, void *data);
125 typedef int (cfs_write_proc_t)(struct file *file, const char *buffer,
126                            unsigned long count, void *data);
127
128 /*
129  * cfs pseudo device
130  *
131  * cfs_psdev_t
132  * cfs_psdev_register:
133  * cfs_psdev_deregister:
134  */
135 typedef struct {
136         int             index;
137         void            *handle;
138         const char      *name;
139         struct cdevsw   *devsw;
140         void            *private;
141 } cfs_psdev_t;
142
143 extern kern_return_t            cfs_psdev_register(cfs_psdev_t *);
144 extern kern_return_t            cfs_psdev_deregister(cfs_psdev_t *);
145
146 /*
147  * Task struct and ...
148  *
149  * Using BSD current_proc in Darwin
150  */
151 extern boolean_t        assert_wait_possible(void);
152 extern void             *get_bsdtask_info(task_t);
153
154 #ifdef __DARWIN8__
155
156 typedef struct {}               cfs_task_t;
157 #define cfs_current()           ((cfs_task_t *)current_thread())
158 #else   /* !__DARWIN8__ */
159
160 typedef struct uthread          cfs_task_t;
161
162 #define current_uthread()       ((struct uthread *)get_bsdthread_info(current_act()))
163 #define cfs_current()           current_uthread()
164
165 #endif /* !__DARWIN8__ */
166
167 #define cfs_task_lock(t)        do {;} while (0)
168 #define cfs_task_unlock(t)      do {;} while (0)
169
170 #define set_current_state(s)    do {;} while (0)
171
172 #define CFS_DECL_JOURNAL_DATA   
173 #define CFS_PUSH_JOURNAL        do {;} while(0)
174 #define CFS_POP_JOURNAL         do {;} while(0)
175
176 /*
177  * Kernel thread:
178  *
179  * OSX kernel thread can not be created with args,
180  * so we have to implement new APIs to create thread with args
181  */
182
183 typedef int (*cfs_thread_t)(void *);
184
185 extern task_t   kernel_task;
186
187 /*
188  * cloning flags, no use in OSX, just copy them from Linux
189  */
190 #define CSIGNAL         0x000000ff      /* signal mask to be sent at exit */
191 #define CLONE_VM        0x00000100      /* set if VM shared between processes */
192 #define CLONE_FS        0x00000200      /* set if fs info shared between processes */
193 #define CLONE_FILES     0x00000400      /* set if open files shared between processes */
194 #define CLONE_SIGHAND   0x00000800      /* set if signal handlers and blocked signals shared */
195 #define CLONE_PID       0x00001000      /* set if pid shared */
196 #define CLONE_PTRACE    0x00002000      /* set if we want to let tracing continue on the child too */
197 #define CLONE_VFORK     0x00004000      /* set if the parent wants the child to wake it up on mm_release */
198 #define CLONE_PARENT    0x00008000      /* set if we want to have the same parent as the cloner */
199 #define CLONE_THREAD    0x00010000      /* Same thread group? */
200 #define CLONE_NEWNS     0x00020000      /* New namespace group? */
201
202 #define CLONE_SIGNAL    (CLONE_SIGHAND | CLONE_THREAD)
203
204 extern cfs_task_t kthread_run(cfs_thread_t func, void *arg,
205                               const char namefmt[], ...);
206
207 /*
208  * Wait Queue implementation
209  *
210  * Like wait_queue in Linux
211  */
212 typedef struct cfs_waitq {
213         struct ksleep_chan wq_ksleep_chan;
214 } cfs_waitq_t;
215
216 typedef struct cfs_waitlink {
217         struct cfs_waitq   *wl_waitq;
218         struct ksleep_link  wl_ksleep_link;
219 } cfs_waitlink_t;
220
221 typedef int cfs_task_state_t;
222
223 #define CFS_TASK_INTERRUPTIBLE  THREAD_ABORTSAFE
224 #define CFS_TASK_UNINT          THREAD_UNINT
225
226 void cfs_waitq_init(struct cfs_waitq *waitq);
227 void cfs_waitlink_init(struct cfs_waitlink *link);
228
229 void cfs_waitq_add(struct cfs_waitq *waitq, struct cfs_waitlink *link);
230 void cfs_waitq_add_exclusive(struct cfs_waitq *waitq,
231                              struct cfs_waitlink *link);
232 void cfs_waitq_del(struct cfs_waitq *waitq, struct cfs_waitlink *link);
233 int  cfs_waitq_active(struct cfs_waitq *waitq);
234
235 void cfs_waitq_signal(struct cfs_waitq *waitq);
236 void cfs_waitq_signal_nr(struct cfs_waitq *waitq, int nr);
237 void cfs_waitq_broadcast(struct cfs_waitq *waitq);
238
239 void cfs_waitq_wait(struct cfs_waitlink *link, cfs_task_state_t state);
240 cfs_duration_t cfs_waitq_timedwait(struct cfs_waitlink *link,
241                                    cfs_task_state_t state, 
242                                    cfs_duration_t timeout);
243
244 /*
245  * Thread schedule APIs.
246  */
247 #define MAX_SCHEDULE_TIMEOUT    ((long)(~0UL>>12))
248 extern void thread_set_timer_deadline(__u64 deadline);
249 extern void thread_cancel_timer(void);
250
251 static inline int cfs_schedule_timeout(int state, int64_t timeout)
252 {
253         int          result;
254         
255 #ifdef __DARWIN8__
256         result = assert_wait((event_t)current_thread(), state);
257 #else
258         result = assert_wait((event_t)current_uthread(), state);
259 #endif
260         if (timeout > 0) {
261                 __u64 expire;
262                 nanoseconds_to_absolutetime(timeout, &expire);
263                 clock_absolutetime_interval_to_deadline(expire, &expire);
264                 thread_set_timer_deadline(expire);
265         }
266         if (result == THREAD_WAITING)
267                 result = thread_block(THREAD_CONTINUE_NULL);
268         if (timeout > 0)
269                 thread_cancel_timer();
270         if (result == THREAD_TIMED_OUT)
271                 result = 0;
272         else
273                 result = 1;
274         return result;
275 }
276
277 #define cfs_schedule()  cfs_schedule_timeout(CFS_TASK_UNINT, CFS_TICK)
278 #define cfs_pause(tick) cfs_schedule_timeout(CFS_TASK_UNINT, tick)
279
280 #define __wait_event(wq, condition)                             \
281 do {                                                            \
282         struct cfs_waitlink __wait;                             \
283                                                                 \
284         cfs_waitlink_init(&__wait);                             \
285         for (;;) {                                              \
286                 cfs_waitq_add(&wq, &__wait);                    \
287                 if (condition)                                  \
288                         break;                                  \
289                 cfs_waitq_wait(&__wait, CFS_TASK_UNINT);        \
290                 cfs_waitq_del(&wq, &__wait);                    \
291         }                                                       \
292         cfs_waitq_del(&wq, &__wait);                            \
293 } while (0)
294
295 #define wait_event(wq, condition)                               \
296 do {                                                            \
297         if (condition)                                          \
298                 break;                                          \
299         __wait_event(wq, condition);                            \
300 } while (0)
301
302 #define __wait_event_interruptible(wq, condition, ex, ret)      \
303 do {                                                            \
304         struct cfs_waitlink __wait;                             \
305                                                                 \
306         cfs_waitlink_init(&__wait);                             \
307         for (;;) {                                              \
308                 if (ex == 0)                                    \
309                         cfs_waitq_add(&wq, &__wait);            \
310                 else                                            \
311                         cfs_waitq_add_exclusive(&wq, &__wait);  \
312                 if (condition)                                  \
313                         break;                                  \
314                 if (!cfs_signal_pending()) {                    \
315                         cfs_waitq_wait(&__wait,                 \
316                                        CFS_TASK_INTERRUPTIBLE); \
317                         cfs_waitq_del(&wq, &__wait);            \
318                         continue;                               \
319                 }                                               \
320                 ret = -ERESTARTSYS;                             \
321                 break;                                          \
322         }                                                       \
323         cfs_waitq_del(&wq, &__wait);                            \
324 } while (0)
325
326 #define wait_event_interruptible(wq, condition)                 \
327 ({                                                              \
328         int __ret = 0;                                          \
329         if (!condition)                                         \
330                 __wait_event_interruptible(wq, condition,       \
331                                            0, __ret);           \
332         __ret;                                                  \
333 })
334
335 #define wait_event_interruptible_exclusive(wq, condition)       \
336 ({                                                              \
337         int __ret = 0;                                          \
338         if (!condition)                                         \
339                 __wait_event_interruptible(wq, condition,       \
340                                            1, __ret);           \
341         __ret;                                                  \
342 })
343
344 #ifndef __DARWIN8__
345 extern void     wakeup_one __P((void * chan));
346 #endif
347 /* only used in tests */
348 #define wake_up_process(p)                                      \
349         do {                                                    \
350                 wakeup_one((caddr_t)p);                         \
351         } while (0)
352         
353 /* used in couple of places */
354 static inline void sleep_on(cfs_waitq_t *waitq)
355 {
356         cfs_waitlink_t link;
357         
358         cfs_waitlink_init(&link);
359         cfs_waitq_add(waitq, &link);
360         cfs_waitq_wait(&link, CFS_TASK_UNINT);
361         cfs_waitq_del(waitq, &link);
362 }
363
364 /*
365  * Signal
366  */
367 typedef sigset_t        cfs_sigset_t;
368
369 /*
370  * Timer
371  */
372 typedef struct cfs_timer {
373         struct ktimer t;
374 } cfs_timer_t;
375
376 #define cfs_init_timer(t)       do {} while(0)
377 void cfs_timer_init(struct cfs_timer *t, void (*func)(unsigned long), void *arg);
378 void cfs_timer_done(struct cfs_timer *t);
379 void cfs_timer_arm(struct cfs_timer *t, cfs_time_t deadline);
380 void cfs_timer_disarm(struct cfs_timer *t);
381 int  cfs_timer_is_armed(struct cfs_timer *t);
382
383 cfs_time_t cfs_timer_deadline(struct cfs_timer *t);
384
385 /*
386  * Ioctl
387  * We don't need to copy out everything in osx
388  */
389 #define cfs_ioctl_data_out(a, d, l)                     \
390         ({                                              \
391                 int __size;                             \
392                 int __rc = 0;                           \
393                 assert((l) >= sizeof(*d));              \
394                 __size = (l) - sizeof(*d);              \
395                 if (__size > 0)                         \
396                         __rc = copy_to_user((void *)a + __size, \
397                              (void *)d + __size,        \
398                              __size);                   \
399                 __rc;                                   \
400         })
401
402 /*
403  * CPU
404  */
405 /* Run in PowerG5 who is PPC64 */
406 #define SMP_CACHE_BYTES                         128
407 #define __cacheline_aligned                     __attribute__((__aligned__(SMP_CACHE_BYTES)))
408 #define NR_CPUS                                 2
409
410 /* 
411  * XXX Liang: patch xnu and export current_processor()?
412  *
413  * #define smp_processor_id()                   current_processor()
414  */
415 #define smp_processor_id()                      0
416 /* XXX smp_call_function is not supported in xnu */
417 #define smp_call_function(f, a, n, w)           do {} while(0)
418 int cfs_online_cpus(void);
419
420 /*
421  * Misc
422  */
423 extern int is_suser(void);
424
425 #ifndef likely
426 #define likely(exp) (exp)
427 #endif
428 #ifndef unlikely
429 #define unlikely(exp) (exp)
430 #endif
431
432 #define lock_kernel()                                   do {} while(0)
433 #define unlock_kernel()                                 do {} while(0)
434
435 #define call_usermodehelper(path, argv, envp, 1)        (0)
436
437 #define cfs_module(name, version, init, fini)                           \
438 extern kern_return_t _start(kmod_info_t *ki, void *data);               \
439 extern kern_return_t _stop(kmod_info_t *ki, void *data);                \
440 __private_extern__ kern_return_t name##_start(kmod_info_t *ki, void *data); \
441 __private_extern__ kern_return_t name##_stop(kmod_info_t *ki, void *data); \
442                                                                         \
443 kmod_info_t KMOD_INFO_NAME = { 0, KMOD_INFO_VERSION, -1,                \
444                                { "com.clusterfs.lustre." #name }, { version }, \
445                                -1, 0, 0, 0, 0, name##_start, name##_stop }; \
446                                                                         \
447 __private_extern__ kmod_start_func_t *_realmain = name##_start;         \
448 __private_extern__ kmod_stop_func_t *_antimain = name##_stop;           \
449 __private_extern__ int _kext_apple_cc = __APPLE_CC__ ;                  \
450                                                                         \
451 kern_return_t name##_start(kmod_info_t *ki, void *d)                    \
452 {                                                                       \
453         return init();                                                  \
454 }                                                                       \
455                                                                         \
456 kern_return_t name##_stop(kmod_info_t *ki, void *d)                     \
457 {                                                                       \
458         fini();                                                         \
459         return KERN_SUCCESS;                                            \
460 }                                                                       \
461                                                                         \
462 /*                                                                      \
463  * to allow semicolon after cfs_module(...)                             \
464  */                                                                     \
465 struct __dummy_ ## name ## _struct {}
466
467 #define inter_module_get(n)                     cfs_symbol_get(n)
468 #define inter_module_put(n)                     cfs_symbol_put(n)
469
470 static inline int request_module(const char *name, ...)
471 {
472         return (-EINVAL);
473 }
474
475 #ifndef __exit
476 #define __exit
477 #endif
478 #ifndef __init
479 #define __init
480 #endif
481
482 #define EXPORT_SYMBOL(s)
483 #define MODULE_AUTHOR(s)
484 #define MODULE_DESCRIPTION(s)
485 #define MODULE_LICENSE(s)
486 #define MODULE_PARM(a, b)
487 #define MODULE_PARM_DESC(a, b)
488
489 #define NR_IRQS                         512
490 #define in_interrupt()                  ml_at_interrupt_context()
491
492 #define KERN_EMERG      "<0>"   /* system is unusable                   */
493 #define KERN_ALERT      "<1>"   /* action must be taken immediately     */
494 #define KERN_CRIT       "<2>"   /* critical conditions                  */
495 #define KERN_ERR        "<3>"   /* error conditions                     */
496 #define KERN_WARNING    "<4>"   /* warning conditions                   */
497 #define KERN_NOTICE     "<5>"   /* normal but significant condition     */
498 #define KERN_INFO       "<6>"   /* informational                        */
499 #define KERN_DEBUG      "<7>"   /* debug-level messages                 */
500
501 static inline long PTR_ERR(const void *ptr)
502 {
503         return (long) ptr;
504 }
505
506 #define ERR_PTR(err)    ((void *)err)
507 #define IS_ERR(p)       ((unsigned long)(p) + 1000 < 1000)
508
509 #else   /* !__KERNEL__ */
510
511 typedef struct cfs_proc_dir_entry {
512         void            *data;
513 } cfs_proc_dir_entry_t;
514
515 #include <libcfs/user-prim.h>
516 #define __WORDSIZE      32
517
518 #endif  /* END __KERNEL__ */
519 /*
520  * Error number
521  */
522 #ifndef EPROTO
523 #define EPROTO          EPROTOTYPE
524 #endif
525 #ifndef EBADR
526 #define EBADR           EBADRPC
527 #endif
528 #ifndef ERESTARTSYS
529 #define ERESTARTSYS     512
530 #endif
531 #ifndef EDEADLOCK
532 #define EDEADLOCK       EDEADLK
533 #endif
534 #ifndef ECOMM
535 #define ECOMM           EINVAL
536 #endif
537 #ifndef ENODATA
538 #define ENODATA         EINVAL
539 #endif
540 #ifndef ENOTSUPP
541 #define ENOTSUPP        EINVAL
542 #endif
543
544 #if BYTE_ORDER == BIG_ENDIAN
545 # define __BIG_ENDIAN
546 #else
547 # define __LITTLE_ENDIAN
548 #endif
549
550 #endif  /* __LIBCFS_DARWIN_CFS_PRIM_H__ */