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