summaryrefslogtreecommitdiff
path: root/open-vm-tools/modules/freebsd/vmblock/os.h
blob: 5528f2a5398f81540761754a81eb224cb93c97d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*********************************************************
 * Copyright (C) 2007 VMware, Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of VMware Inc. nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission of VMware Inc.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *********************************************************/


/*
 * os.h --
 *
 *      FreeBSD-specific definitions.
 */


#ifndef __OS_H__
#define __OS_H__

#include <sys/param.h>
#include <sys/queue.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/sx.h>
#include <sys/proc.h>
#include <sys/condvar.h>
#include <vm/uma.h>
#include <machine/stdarg.h>

#include "vm_basic_types.h"

typedef struct sx os_rwlock_t;

typedef struct uma_zone os_kmem_cache_t;
typedef struct os_completion_t {
   Bool completed;
   struct mtx mutex;
   struct cv cv;
} os_completion_t;
/*
 * Changing the os_atomic_t type requires that the os_atomic_* macros below be
 * changed as well.
 */
typedef unsigned int os_atomic_t;
typedef struct file * os_blocker_id_t;

#define OS_UNKNOWN_BLOCKER              NULL
#define OS_ENOMEM                       ENOMEM
#define OS_ENOENT                       ENOENT
#define OS_EEXIST                       EEXIST
#define OS_PATH_MAX                     MAXPATHLEN
#define OS_KMEM_CACHE_FLAG_HWALIGN      0       // unused

#define OS_FMTTID                       "p"
#define os_threadid                     curthread

extern NORETURN void os_panic(const char *fmt, va_list args);

#define os_rwlock_init(lock)            sx_init(lock, "vmblock-sx")
#define os_rwlock_destroy(lock)         sx_destroy(lock)
#define os_assert_rwlock_held(lock)     sx_assert(lock, SX_LOCKED)
#define os_read_lock(lock)              sx_slock(lock)
#define os_write_lock(lock)             sx_xlock(lock)
#define os_read_unlock(lock)            sx_sunlock(lock)
#define os_write_unlock(lock)           sx_xunlock(lock)

/*
 * XXX linux/os.h requests alignment on HW cache lines.  Is this of
 * serious concern?  We can do that by using UMA_ALIGN_CACHE as the
 * 'align' parameter to uma_zalloc, but with slightly different
 * semantics, it sort of changes the name of the game.
 */
#define os_kmem_cache_create(name, size, align, ctor) \
   uma_zcreate(name, size, ctor, NULL, NULL, NULL, align, 0)
#define os_kmem_cache_destroy(cache)    uma_zdestroy(cache)
#define os_kmem_cache_alloc(cache)      uma_zalloc(cache, M_WAITOK)
#define os_kmem_cache_free(cache, elem) uma_zfree(cache, elem)

#define os_completion_init(comp)                                \
      do {                                                      \
         (comp)->completed = FALSE;                             \
         mtx_init(&(comp)->mutex, "vmblock-mtx", "vmblock-mtx", MTX_DEF);         \
         cv_init(&(comp)->cv, "vmblock-cv");                       \
      } while (0)
#define os_completion_destroy(comp)                             \
      do {                                                      \
         mtx_destroy(&(comp)->mutex);                           \
         cv_destroy(&(comp)->cv);                               \
      } while (0)
/*
 * This macro evaluates to non-zero only if cv_wait_sig is interrupted.
 */
#define os_wait_for_completion(comp)                            \
({                                                              \
         int error = 0;                                         \
         mtx_lock(&(comp)->mutex);                              \
         while (!(comp)->completed && !error) {                 \
            error = cv_wait_sig(&(comp)->cv, &(comp)->mutex);   \
         }                                                      \
         mtx_unlock(&(comp)->mutex);                            \
         error;                                                 \
})
#define os_complete_all(comp)                                   \
      do {                                                      \
         mtx_lock(&(comp)->mutex);                              \
         (comp)->completed = TRUE;                              \
         cv_broadcast(&(comp)->cv);                             \
         mtx_unlock(&(comp)->mutex);                            \
      } while (0)

/* atomic_fetchadd_int returns the value of atomic before addition. */
#define os_atomic_dec_and_test(atomic)  (atomic_fetchadd_int(atomic, -1) == 1)
#define os_atomic_dec(atomic)           atomic_subtract_int(atomic, 1)
#define os_atomic_inc(atomic)           atomic_add_int(atomic, 1)
#define os_atomic_set(atomic, val)      atomic_store_rel_int(atomic, val)
#define os_atomic_read(atomic)          atomic_load_acq_int(atomic)

#endif /* __OS_H__ */