1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef nspr_freebsd_defs_h___
7 #define nspr_freebsd_defs_h___
8 
9 #include "prthread.h"
10 
11 #if __FreeBSD__ >= 2
12 #include <osreldate.h>  /* for __FreeBSD_version */
13 #endif
14 #include <sys/syscall.h>
15 
16 #define PR_LINKER_ARCH  "freebsd"
17 #define _PR_SI_SYSNAME  "FREEBSD"
18 #if defined(__i386__)
19 #define _PR_SI_ARCHITECTURE "x86"
20 #elif defined(__alpha__)
21 #define _PR_SI_ARCHITECTURE "alpha"
22 #elif defined(__sparc__)
23 #define _PR_SI_ARCHITECTURE "sparc"
24 #elif defined(__ia64__)
25 #define _PR_SI_ARCHITECTURE "ia64"
26 #elif defined(__amd64__)
27 #define _PR_SI_ARCHITECTURE "amd64"
28 #elif defined(__powerpc64__)
29 #define _PR_SI_ARCHITECTURE "powerpc64"
30 #elif defined(__powerpc__)
31 #define _PR_SI_ARCHITECTURE "powerpc"
32 #elif defined(__aarch64__)
33 #define _PR_SI_ARCHITECTURE "aarch64"
34 #elif defined(__arm__)
35 #define _PR_SI_ARCHITECTURE "arm"
36 #elif defined(__mips64__)
37 #define _PR_SI_ARCHITECTURE "mips64"
38 #elif defined(__mips__)
39 #define _PR_SI_ARCHITECTURE "mips"
40 #else
41 #error "Unknown CPU architecture"
42 #endif
43 #if defined(__ELF__)
44 #define PR_DLL_SUFFIX          ".so"
45 #else
46 #define PR_DLL_SUFFIX          ".so.1.0"
47 #endif
48 
49 #define _PR_VMBASE              0x30000000
50 #define _PR_STACK_VMBASE    0x50000000
51 #define _MD_DEFAULT_STACK_SIZE  65536L
52 #define _MD_MMAP_FLAGS          MAP_PRIVATE
53 
54 #undef  HAVE_STACK_GROWING_UP
55 #define HAVE_DLL
56 #define USE_DLFCN
57 #define _PR_HAVE_SOCKADDR_LEN
58 #define _PR_STAT_HAS_ST_ATIMESPEC
59 #define _PR_HAVE_LARGE_OFF_T
60 
61 #if defined(_PR_PTHREADS)
62 #if __FreeBSD_version >= 400008
63 /*
64  * libc_r before this version of FreeBSD doesn't have poll().
65  * Although libc has poll(), it is not thread-safe so we can't
66  * use it in the pthreads version.
67  */
68 #define _PR_POLL_AVAILABLE
69 #endif
70 #else
71 #if __FreeBSD_version >= 300000
72 #define _PR_POLL_AVAILABLE
73 #define _PR_USE_POLL
74 #endif
75 #endif
76 
77 #define _PR_HAVE_SYSV_SEMAPHORES
78 #define PR_HAVE_SYSV_NAMED_SHARED_MEMORY
79 
80 #if __FreeBSD_version >= 400014
81 #define _PR_INET6
82 #define _PR_HAVE_INET_NTOP
83 #define _PR_HAVE_GETHOSTBYNAME2
84 #define _PR_HAVE_GETADDRINFO
85 #define _PR_INET6_PROBE
86 #define _PR_IPV6_V6ONLY_PROBE
87 #endif
88 
89 #define USE_SETJMP
90 
91 #ifndef _PR_PTHREADS
92 #include <setjmp.h>
93 
94 #define PR_CONTEXT_TYPE sigjmp_buf
95 
96 #define CONTEXT(_th) ((_th)->md.context)
97 
98 #define _MD_GET_SP(_th)    (_th)->md.context[0]._sjb[2]
99 #define PR_NUM_GCREGS   _JBLEN
100 
101 /*
102 ** Initialize a thread context to run "_main()" when started
103 */
104 #define _MD_INIT_CONTEXT(_thread, _sp, _main, status)  \
105 {  \
106     *status = PR_TRUE;  \
107     if (sigsetjmp(CONTEXT(_thread), 1)) {  \
108         _main();  \
109     }  \
110     _MD_GET_SP(_thread) = (unsigned char*) ((_sp) - 64); \
111 }
112 
113 #define _MD_SWITCH_CONTEXT(_thread)  \
114     if (!sigsetjmp(CONTEXT(_thread), 1)) {  \
115     (_thread)->md.errcode = errno;  \
116     _PR_Schedule();  \
117     }
118 
119 /*
120 ** Restore a thread context, saved by _MD_SWITCH_CONTEXT
121 */
122 #define _MD_RESTORE_CONTEXT(_thread) \
123 {   \
124     errno = (_thread)->md.errcode;  \
125     _MD_SET_CURRENT_THREAD(_thread);  \
126     siglongjmp(CONTEXT(_thread), 1);  \
127 }
128 
129 /* Machine-dependent (MD) data structures */
130 
131 struct _MDThread {
132     PR_CONTEXT_TYPE context;
133     int id;
134     int errcode;
135 };
136 
137 struct _MDThreadStack {
138     PRInt8 notused;
139 };
140 
141 struct _MDLock {
142     PRInt8 notused;
143 };
144 
145 struct _MDSemaphore {
146     PRInt8 notused;
147 };
148 
149 struct _MDCVar {
150     PRInt8 notused;
151 };
152 
153 struct _MDSegment {
154     PRInt8 notused;
155 };
156 
157 /*
158  * md-specific cpu structure field
159  */
160 #define _PR_MD_MAX_OSFD FD_SETSIZE
161 
162 struct _MDCPU_Unix {
163     PRCList ioQ;
164     PRUint32 ioq_timeout;
165     PRInt32 ioq_max_osfd;
166     PRInt32 ioq_osfd_cnt;
167 #ifndef _PR_USE_POLL
168     fd_set fd_read_set, fd_write_set, fd_exception_set;
169     PRInt16 fd_read_cnt[_PR_MD_MAX_OSFD],fd_write_cnt[_PR_MD_MAX_OSFD],
170             fd_exception_cnt[_PR_MD_MAX_OSFD];
171 #else
172     struct pollfd *ioq_pollfds;
173     int ioq_pollfds_size;
174 #endif  /* _PR_USE_POLL */
175 };
176 
177 #define _PR_IOQ(_cpu)           ((_cpu)->md.md_unix.ioQ)
178 #define _PR_ADD_TO_IOQ(_pq, _cpu) PR_APPEND_LINK(&_pq.links, &_PR_IOQ(_cpu))
179 #define _PR_FD_READ_SET(_cpu)       ((_cpu)->md.md_unix.fd_read_set)
180 #define _PR_FD_READ_CNT(_cpu)       ((_cpu)->md.md_unix.fd_read_cnt)
181 #define _PR_FD_WRITE_SET(_cpu)      ((_cpu)->md.md_unix.fd_write_set)
182 #define _PR_FD_WRITE_CNT(_cpu)      ((_cpu)->md.md_unix.fd_write_cnt)
183 #define _PR_FD_EXCEPTION_SET(_cpu)  ((_cpu)->md.md_unix.fd_exception_set)
184 #define _PR_FD_EXCEPTION_CNT(_cpu)  ((_cpu)->md.md_unix.fd_exception_cnt)
185 #define _PR_IOQ_TIMEOUT(_cpu)       ((_cpu)->md.md_unix.ioq_timeout)
186 #define _PR_IOQ_MAX_OSFD(_cpu)      ((_cpu)->md.md_unix.ioq_max_osfd)
187 #define _PR_IOQ_OSFD_CNT(_cpu)      ((_cpu)->md.md_unix.ioq_osfd_cnt)
188 #define _PR_IOQ_POLLFDS(_cpu)       ((_cpu)->md.md_unix.ioq_pollfds)
189 #define _PR_IOQ_POLLFDS_SIZE(_cpu)  ((_cpu)->md.md_unix.ioq_pollfds_size)
190 
191 #define _PR_IOQ_MIN_POLLFDS_SIZE(_cpu)  32
192 
193 struct _MDCPU {
194     struct _MDCPU_Unix md_unix;
195 };
196 
197 #define _MD_INIT_LOCKS()
198 #define _MD_NEW_LOCK(lock) PR_SUCCESS
199 #define _MD_FREE_LOCK(lock)
200 #define _MD_LOCK(lock)
201 #define _MD_UNLOCK(lock)
202 #define _MD_INIT_IO()
203 #define _MD_IOQ_LOCK()
204 #define _MD_IOQ_UNLOCK()
205 
206 extern PRStatus _MD_InitializeThread(PRThread *thread);
207 
208 #define _MD_INIT_RUNNING_CPU(cpu)       _MD_unix_init_running_cpu(cpu)
209 #define _MD_INIT_THREAD                 _MD_InitializeThread
210 #define _MD_EXIT_THREAD(thread)
211 #define _MD_SUSPEND_THREAD(thread)      _MD_suspend_thread
212 #define _MD_RESUME_THREAD(thread)       _MD_resume_thread
213 #define _MD_CLEAN_THREAD(_thread)
214 
215 extern PRStatus _MD_CREATE_THREAD(
216     PRThread *thread,
217     void (*start) (void *),
218     PRThreadPriority priority,
219     PRThreadScope scope,
220     PRThreadState state,
221     PRUint32 stackSize);
222 extern void _MD_SET_PRIORITY(struct _MDThread *thread, PRUintn newPri);
223 extern PRStatus _MD_WAIT(PRThread *, PRIntervalTime timeout);
224 extern PRStatus _MD_WAKEUP_WAITER(PRThread *);
225 extern void _MD_YIELD(void);
226 
227 #endif /* ! _PR_PTHREADS */
228 
229 extern void _MD_EarlyInit(void);
230 
231 #define _MD_EARLY_INIT                  _MD_EarlyInit
232 #define _MD_FINAL_INIT          _PR_UnixInit
233 #define _PR_HAVE_CLOCK_MONOTONIC
234 
235 /*
236  * We wrapped the select() call.  _MD_SELECT refers to the built-in,
237  * unwrapped version.
238  */
239 #define _MD_SELECT(nfds,r,w,e,tv) syscall(SYS_select,nfds,r,w,e,tv)
240 
241 #if defined(_PR_POLL_AVAILABLE)
242 #include <poll.h>
243 #define _MD_POLL(fds,nfds,timeout) syscall(SYS_poll,fds,nfds,timeout)
244 #endif
245 
246 /* freebsd has INADDR_LOOPBACK defined, but in /usr/include/rpc/types.h, and I didn't
247    want to be including that.. */
248 #ifndef INADDR_LOOPBACK
249 #define INADDR_LOOPBACK         (u_long)0x7F000001
250 #endif
251 
252 /* For writev() */
253 #include <sys/uio.h>
254 
255 #endif /* nspr_freebsd_defs_h___ */
256