1 #ifndef __LIBCFS_DARWIN_CFS_MEM_H__
2 #define __LIBCFS_DARWIN_CFS_MEM_H__
4 #ifndef __LIBCFS_LIBCFS_H__
5 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
10 #include <sys/types.h>
11 #include <sys/systm.h>
14 #include <sys/kernel.h>
17 #include <sys/malloc.h>
19 #include <sys/lockf.h>
21 #include <mach/mach_types.h>
22 #include <mach/vm_types.h>
24 #include <vm/vm_kern.h>
25 #include <mach/machine/vm_param.h>
26 #include <kern/thread_call.h>
27 #include <sys/param.h>
30 #include <libcfs/darwin/darwin-types.h>
31 #include <libcfs/darwin/darwin-sync.h>
32 #include <libcfs/darwin/darwin-lock.h>
33 #include <libcfs/list.h>
36 * Basic xnu_page struct, should be binary compatibility with
37 * all page types in xnu (we have only xnu_raw_page, xll_page now)
40 /* Variable sized pages are not supported */
43 #define CFS_PAGE_SHIFT PAGE_SHIFT
45 #define CFS_PAGE_SHIFT 12
48 #define CFS_PAGE_SIZE (1UL << CFS_PAGE_SHIFT)
50 #define CFS_PAGE_MASK (~((__u64)CFS_PAGE_SIZE - 1))
58 typedef __u32 page_off_t;
61 * For XNU we have our own page cache built on top of underlying BSD/MACH
62 * infrastructure. In particular, we have two disjoint types of pages:
64 * - "raw" pages (XNU_PAGE_RAW): these are just buffers mapped into KVM,
67 * - "xll" pages (XNU_PAGE_XLL): these are used by file system to cache
68 * file data, owned by file system objects, hashed, lrued, etc.
70 * cfs_page_t has to cover both of them, because core Lustre code is based on
71 * the Linux assumption that page is _both_ memory buffer and file system
74 * To achieve this, all types of pages supported on XNU has to start from
75 * common header that contains only "page type". Common cfs_page_t operations
76 * dispatch through operation vector based on page type.
79 typedef struct xnu_page {
84 void *(*page_map) (cfs_page_t *);
85 void (*page_unmap) (cfs_page_t *);
86 void *(*page_address) (cfs_page_t *);
89 void xnu_page_ops_register(int type, struct xnu_page_ops *ops);
90 void xnu_page_ops_unregister(int type);
93 * raw page, no cache object, just like buffer
96 struct xnu_page header;
99 struct list_head link;
103 * Public interface to lustre
105 * - cfs_alloc_page(f)
109 * - cfs_page_address(p)
113 * Of all functions above only cfs_kmap(), cfs_kunmap(), and
114 * cfs_page_address() can be called on file system pages. The rest is for raw
118 cfs_page_t *cfs_alloc_page(u_int32_t flags);
119 void cfs_free_page(cfs_page_t *page);
120 void cfs_get_page(cfs_page_t *page);
121 int cfs_put_page_testzero(cfs_page_t *page);
122 int cfs_page_count(cfs_page_t *page);
123 #define cfs_page_index(pg) (0)
125 void *cfs_page_address(cfs_page_t *pg);
126 void *cfs_kmap(cfs_page_t *pg);
127 void cfs_kunmap(cfs_page_t *pg);
133 void *cfs_alloc(size_t nr_bytes, u_int32_t flags);
134 void cfs_free(void *addr);
136 void *cfs_alloc_large(size_t nr_bytes);
137 void cfs_free_large(void *addr);
139 extern int get_preemption_level(void);
141 #define CFS_ALLOC_ATOMIC_TRY \
142 (get_preemption_level() != 0 ? CFS_ALLOC_ATOMIC : 0)
147 * No slab in OSX, use zone allocator to simulate slab
149 #define SLAB_HWCACHE_ALIGN 0
153 * In Darwin8, we cannot use zalloc_noblock(not exported by kernel),
154 * also, direct using of zone allocator is not recommended.
156 #define CFS_INDIVIDUAL_ZONE (0)
158 #if !CFS_INDIVIDUAL_ZONE
159 #include <libkern/OSMalloc.h>
160 typedef OSMallocTag mem_cache_t;
162 typedef void* zone_t;
163 typedef zone_t mem_cache_t;
166 #else /* !__DARWIN8__ */
168 #define CFS_INDIVIDUAL_ZONE (1)
170 typedef zone_t mem_cache_t;
172 #endif /* !__DARWIN8__ */
174 #define MC_NAME_MAX_LEN 64
176 typedef struct cfs_mem_cache {
178 mem_cache_t mc_cache;
179 struct list_head mc_link;
180 char mc_name [MC_NAME_MAX_LEN];
183 #define KMEM_CACHE_MAX_COUNT 64
184 #define KMEM_MAX_ZONE 8192
186 cfs_mem_cache_t * cfs_mem_cache_create (const char *, size_t, size_t, unsigned long);
187 int cfs_mem_cache_destroy ( cfs_mem_cache_t * );
188 void *cfs_mem_cache_alloc ( cfs_mem_cache_t *, int);
189 void cfs_mem_cache_free ( cfs_mem_cache_t *, void *);
194 /* XXX Liang: num_physpages... fix me */
195 #define num_physpages (64 * 1024)
197 #define CFS_DECL_MMSPACE
198 #define CFS_MMSPACE_OPEN do {} while(0)
199 #define CFS_MMSPACE_CLOSE do {} while(0)
201 #define copy_from_user(kaddr, uaddr, size) copyin(CAST_USER_ADDR_T(uaddr), (caddr_t)kaddr, size)
202 #define copy_to_user(uaddr, kaddr, size) copyout((caddr_t)kaddr, CAST_USER_ADDR_T(uaddr), size)
205 static inline int strncpy_from_user(char *kaddr, char *uaddr, int size)
208 return copyinstr((const user_addr_t)uaddr, (void *)kaddr, size, &count);
212 #if defined (__ppc__)
213 #define mb() __asm__ __volatile__ ("sync" : : : "memory")
214 #define rmb() __asm__ __volatile__ ("sync" : : : "memory")
215 #define wmb() __asm__ __volatile__ ("eieio" : : : "memory")
216 #elif defined (__i386__)
217 #define mb() __asm__ __volatile__ ("lock; addl $0,0(%%esp)": : :"memory")
219 #define wmb() __asm__ __volatile__ ("": : :"memory")
221 #error architecture not supported
224 #else /* !__KERNEL__ */
226 #define CFS_CACHE_SHIFT 12
227 #define PAGE_CACHE_SIZE (1 << CFS_CACHE_SHIFT)
228 #include <libcfs/user-prim.h>
230 #endif /* __KERNEL__ */
232 #endif /* __XNU_CFS_MEM_H__ */