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_linux_defs_h___
36#define nspr_linux_defs_h___
37
38#include "prthread.h"
39
40/*
41 * Internal configuration macros
42 */
43
44#define PR_LINKER_ARCH	"linux"
45#define _PR_SI_SYSNAME  "LINUX"
46#ifdef __powerpc__
47#define _PR_SI_ARCHITECTURE "ppc"
48#elif defined(__alpha)
49#define _PR_SI_ARCHITECTURE "alpha"
50#elif defined(__ia64__)
51#define _PR_SI_ARCHITECTURE "ia64"
52#elif defined(__x86_64__)
53#define _PR_SI_ARCHITECTURE "x86-64"
54#elif defined(__mc68000__)
55#define _PR_SI_ARCHITECTURE "m68k"
56#elif defined(__sparc__)
57#define _PR_SI_ARCHITECTURE "sparc"
58#elif defined(__i386__)
59#define _PR_SI_ARCHITECTURE "x86"
60#elif defined(__mips__)
61#define _PR_SI_ARCHITECTURE "mips"
62#elif defined(__arm__)
63#define _PR_SI_ARCHITECTURE "arm"
64#elif defined(__hppa__)
65#define _PR_SI_ARCHITECTURE "hppa"
66#elif defined(__s390__)
67#define _PR_SI_ARCHITECTURE "s390"
68#elif defined(__s390x__)
69#define _PR_SI_ARCHITECTURE "s390x"
70#else
71#error "Unknown CPU architecture"
72#endif
73#define PR_DLL_SUFFIX		".so"
74
75#define _PR_VMBASE              0x30000000
76#define _PR_STACK_VMBASE	0x50000000
77#define _MD_DEFAULT_STACK_SIZE	65536L
78#define _MD_MMAP_FLAGS          MAP_PRIVATE
79
80#undef	HAVE_STACK_GROWING_UP
81
82/*
83 * Elf linux supports dl* functions
84 */
85#define HAVE_DLL
86#define USE_DLFCN
87
88#if defined(__i386__)
89#define _PR_HAVE_ATOMIC_OPS
90#define _MD_INIT_ATOMIC()
91extern PRInt32 _PR_x86_AtomicIncrement(PRInt32 *val);
92#define _MD_ATOMIC_INCREMENT          _PR_x86_AtomicIncrement
93extern PRInt32 _PR_x86_AtomicDecrement(PRInt32 *val);
94#define _MD_ATOMIC_DECREMENT          _PR_x86_AtomicDecrement
95extern PRInt32 _PR_x86_AtomicAdd(PRInt32 *ptr, PRInt32 val);
96#define _MD_ATOMIC_ADD                _PR_x86_AtomicAdd
97extern PRInt32 _PR_x86_AtomicSet(PRInt32 *val, PRInt32 newval);
98#define _MD_ATOMIC_SET                _PR_x86_AtomicSet
99#endif
100
101#if defined(__ia64__)
102#define _PR_HAVE_ATOMIC_OPS
103#define _MD_INIT_ATOMIC()
104extern PRInt32 _PR_ia64_AtomicIncrement(PRInt32 *val);
105#define _MD_ATOMIC_INCREMENT          _PR_ia64_AtomicIncrement
106extern PRInt32 _PR_ia64_AtomicDecrement(PRInt32 *val);
107#define _MD_ATOMIC_DECREMENT          _PR_ia64_AtomicDecrement
108extern PRInt32 _PR_ia64_AtomicAdd(PRInt32 *ptr, PRInt32 val);
109#define _MD_ATOMIC_ADD                _PR_ia64_AtomicAdd
110extern PRInt32 _PR_ia64_AtomicSet(PRInt32 *val, PRInt32 newval);
111#define _MD_ATOMIC_SET                _PR_ia64_AtomicSet
112#endif
113
114#if defined(__x86_64__)
115#define _PR_HAVE_ATOMIC_OPS
116#define _MD_INIT_ATOMIC()
117extern PRInt32 _PR_x86_64_AtomicIncrement(PRInt32 *val);
118#define _MD_ATOMIC_INCREMENT          _PR_x86_64_AtomicIncrement
119extern PRInt32 _PR_x86_64_AtomicDecrement(PRInt32 *val);
120#define _MD_ATOMIC_DECREMENT          _PR_x86_64_AtomicDecrement
121extern PRInt32 _PR_x86_64_AtomicAdd(PRInt32 *ptr, PRInt32 val);
122#define _MD_ATOMIC_ADD                _PR_x86_64_AtomicAdd
123extern PRInt32 _PR_x86_64_AtomicSet(PRInt32 *val, PRInt32 newval);
124#define _MD_ATOMIC_SET                _PR_x86_64_AtomicSet
125#endif
126
127#define USE_SETJMP
128#if defined(__GLIBC__) && __GLIBC__ >= 2
129#define _PR_POLL_AVAILABLE
130#endif
131#undef _PR_USE_POLL
132#define _PR_STAT_HAS_ONLY_ST_ATIME
133#if defined(__alpha) || defined(__ia64__)
134#define _PR_HAVE_LARGE_OFF_T
135#elif (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1)
136#define _PR_HAVE_OFF64_T
137#else
138#define _PR_NO_LARGE_FILES
139#endif
140#if (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1)
141#define _PR_INET6
142#define _PR_HAVE_INET_NTOP
143#define _PR_HAVE_GETHOSTBYNAME2
144#define _PR_HAVE_GETADDRINFO
145#define _PR_INET6_PROBE
146#endif
147#define _PR_HAVE_SYSV_SEMAPHORES
148#define PR_HAVE_SYSV_NAMED_SHARED_MEMORY
149#if (__GLIBC__ >= 2) && defined(_PR_PTHREADS)
150#define _PR_HAVE_GETHOST_R
151#define _PR_HAVE_GETHOST_R_INT
152#endif
153
154#ifdef _PR_PTHREADS
155
156extern void _MD_CleanupBeforeExit(void);
157#define _MD_CLEANUP_BEFORE_EXIT _MD_CleanupBeforeExit
158
159#else  /* ! _PR_PTHREADS */
160
161#include <setjmp.h>
162
163#define PR_CONTEXT_TYPE	sigjmp_buf
164
165#define CONTEXT(_th) ((_th)->md.context)
166
167#ifdef __powerpc__
168/*
169 * PowerPC based MkLinux
170 *
171 * On the PowerPC, the new style jmp_buf isn't used until glibc
172 * 2.1.
173 */
174#if (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1)
175#define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[JB_GPR1]
176#else
177#define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[0].__misc[0]
178#endif /* glibc 2.1 or later */
179#define _MD_SET_FP(_t, val)
180#define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
181#define _MD_GET_FP_PTR(_t) ((void *) 0)
182/* aix = 64, macos = 70 */
183#define PR_NUM_GCREGS  64
184
185#elif defined(__alpha)
186/* Alpha based Linux */
187
188#if defined(__GLIBC__) && __GLIBC__ >= 2
189#define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[JB_SP]
190#define _MD_SET_FP(_t, val)
191#define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
192#define _MD_GET_FP_PTR(_t) ((void *) 0)
193#define _MD_SP_TYPE long int
194#else
195#define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[0].__sp
196#define _MD_SET_FP(_t, val)
197#define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
198#define _MD_GET_FP_PTR(_t) ((void *) 0)
199#define _MD_SP_TYPE __ptr_t
200#endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */
201
202/* XXX not sure if this is correct, or maybe it should be 17? */
203#define PR_NUM_GCREGS 9
204
205#elif defined(__ia64__)
206
207#define _MD_GET_SP(_t)      ((long *)((_t)->md.context[0].__jmpbuf)[0])
208#define _MD_SET_FP(_t, val)
209#define _MD_GET_SP_PTR(_t)  &(_MD_GET_SP(_t))
210#define _MD_GET_FP_PTR(_t)  ((void *) 0)
211#define _MD_SP_TYPE         long int
212
213#define PR_NUM_GCREGS       _JBLEN
214
215#elif defined(__mc68000__)
216/* m68k based Linux */
217
218/*
219 * On the m68k, glibc still uses the old style sigjmp_buf, even
220 * in glibc 2.0.7.
221 */
222#if defined(__GLIBC__) && __GLIBC__ >= 2
223#define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[0].__sp
224#define _MD_SET_FP(_t, val)
225#define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
226#define _MD_GET_FP_PTR(_t) ((void *) 0)
227#define _MD_SP_TYPE int
228#else
229#define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[0].__sp
230#define _MD_SET_FP(_t, val)
231#define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
232#define _MD_GET_FP_PTR(_t) ((void *) 0)
233#define _MD_SP_TYPE __ptr_t
234#endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */
235
236/* XXX not sure if this is correct, or maybe it should be 17? */
237#define PR_NUM_GCREGS 9
238
239#elif defined(__sparc__)
240/* Sparc */
241#if defined(__GLIBC__) && __GLIBC__ >= 2
242/*
243 * You need glibc2-2.0.7-25 or later. The libraries that came with
244 * Red Hat 5.1 are not new enough, but they are in 5.2.
245 */
246#define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[JB_SP]
247#define _MD_SET_FP(_t, val) ((_t)->md.context[0].__jmpbuf[JB_FP] = val)
248#define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
249#define _MD_GET_FP_PTR(_t) (&(_t)->md.context[0].__jmpbuf[JB_FP])
250#define _MD_SP_TYPE int
251#else
252#define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[0].__fp
253#define _MD_SET_FP(_t, val)
254#define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
255#define _MD_GET_FP_PTR(_t) ((void *) 0)
256#define _MD_SP_TYPE __ptr_t
257#endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */
258
259#elif defined(__i386__)
260/* Intel based Linux */
261#if defined(__GLIBC__) && __GLIBC__ >= 2
262#define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[JB_SP]
263#define _MD_SET_FP(_t, val) ((_t)->md.context[0].__jmpbuf[JB_BP] = val)
264#define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
265#define _MD_GET_FP_PTR(_t) (&(_t)->md.context[0].__jmpbuf[JB_BP])
266#define _MD_SP_TYPE int
267#else
268#define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[0].__sp
269#define _MD_SET_FP(_t, val) ((_t)->md.context[0].__jmpbuf[0].__bp = val)
270#define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
271#define _MD_GET_FP_PTR(_t) &((_t)->md.context[0].__jmpbuf[0].__bp)
272#define _MD_SP_TYPE __ptr_t
273#endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */
274#define PR_NUM_GCREGS   6
275
276#elif defined(__mips__)
277/* Linux/MIPS */
278#if defined(__GLIBC__) && __GLIBC__ >= 2
279#define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[0].__sp
280#define _MD_SET_FP(_t, val) ((_t)->md.context[0].__jmpbuf[0].__fp = (val))
281#define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
282#define _MD_GET_FP_PTR(_t) (&(_t)->md.context[0].__jmpbuf[0].__fp)
283#define _MD_SP_TYPE __ptr_t
284#else
285#error "Linux/MIPS pre-glibc2 not supported yet"
286#endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */
287
288#elif defined(__arm__)
289/* ARM/Linux */
290#if defined(__GLIBC__) && __GLIBC__ >= 2
291#define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[20]
292#define _MD_SET_FP(_t, val) ((_t)->md.context[0].__jmpbuf[19] = (val))
293#define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
294#define _MD_GET_FP_PTR(_t) (&(_t)->md.context[0].__jmpbuf[19])
295#define _MD_SP_TYPE __ptr_t
296#else
297#error "ARM/Linux pre-glibc2 not supported yet"
298#endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */
299
300#else
301
302#error "Unknown CPU architecture"
303
304#endif /*__powerpc__*/
305
306/*
307** Initialize a thread context to run "_main()" when started
308*/
309#ifdef __powerpc__
310
311#define _MD_INIT_CONTEXT(_thread, _sp, _main, status)  \
312{  \
313    *status = PR_TRUE;  \
314    if (sigsetjmp(CONTEXT(_thread), 1)) {  \
315        _main();  \
316    }  \
317    _MD_GET_SP(_thread) = (unsigned char*) ((_sp) - 128); \
318	_thread->md.sp = _MD_GET_SP_PTR(_thread); \
319	_thread->md.fp = _MD_GET_FP_PTR(_thread); \
320    _MD_SET_FP(_thread, 0); \
321}
322
323#elif defined(__mips__)
324
325#define _MD_INIT_CONTEXT(_thread, _sp, _main, status)  \
326{  \
327    *status = PR_TRUE;  \
328    (void) sigsetjmp(CONTEXT(_thread), 1);  \
329    _thread->md.context[0].__jmpbuf[0].__pc = (__ptr_t) _main;  \
330    _MD_GET_SP(_thread) = (_MD_SP_TYPE) ((_sp) - 64); \
331    _thread->md.sp = _MD_GET_SP_PTR(_thread); \
332    _thread->md.fp = _MD_GET_FP_PTR(_thread); \
333    _MD_SET_FP(_thread, 0); \
334}
335
336#else
337
338#define _MD_INIT_CONTEXT(_thread, _sp, _main, status)  \
339{  \
340    *status = PR_TRUE;  \
341    if (sigsetjmp(CONTEXT(_thread), 1)) {  \
342        _main();  \
343    }  \
344    _MD_GET_SP(_thread) = (_MD_SP_TYPE) ((_sp) - 64); \
345	_thread->md.sp = _MD_GET_SP_PTR(_thread); \
346	_thread->md.fp = _MD_GET_FP_PTR(_thread); \
347    _MD_SET_FP(_thread, 0); \
348}
349
350#endif /*__powerpc__*/
351
352#define _MD_SWITCH_CONTEXT(_thread)  \
353    if (!sigsetjmp(CONTEXT(_thread), 1)) {  \
354	(_thread)->md.errcode = errno;  \
355	_PR_Schedule();  \
356    }
357
358/*
359** Restore a thread context, saved by _MD_SWITCH_CONTEXT
360*/
361#define _MD_RESTORE_CONTEXT(_thread) \
362{   \
363    errno = (_thread)->md.errcode;  \
364    _MD_SET_CURRENT_THREAD(_thread);  \
365    siglongjmp(CONTEXT(_thread), 1);  \
366}
367
368/* Machine-dependent (MD) data structures */
369
370struct _MDThread {
371    PR_CONTEXT_TYPE context;
372	void *sp;
373	void *fp;
374    int id;
375    int errcode;
376};
377
378struct _MDThreadStack {
379    PRInt8 notused;
380};
381
382struct _MDLock {
383    PRInt8 notused;
384};
385
386struct _MDSemaphore {
387    PRInt8 notused;
388};
389
390struct _MDCVar {
391    PRInt8 notused;
392};
393
394struct _MDSegment {
395    PRInt8 notused;
396};
397
398/*
399 * md-specific cpu structure field
400 */
401#include <sys/time.h>  /* for FD_SETSIZE */
402#define _PR_MD_MAX_OSFD FD_SETSIZE
403
404struct _MDCPU_Unix {
405    PRCList ioQ;
406    PRUint32 ioq_timeout;
407    PRInt32 ioq_max_osfd;
408    PRInt32 ioq_osfd_cnt;
409#ifndef _PR_USE_POLL
410    fd_set fd_read_set, fd_write_set, fd_exception_set;
411    PRInt16 fd_read_cnt[_PR_MD_MAX_OSFD],fd_write_cnt[_PR_MD_MAX_OSFD],
412				fd_exception_cnt[_PR_MD_MAX_OSFD];
413#else
414	struct pollfd *ioq_pollfds;
415	int ioq_pollfds_size;
416#endif	/* _PR_USE_POLL */
417};
418
419#define _PR_IOQ(_cpu)			((_cpu)->md.md_unix.ioQ)
420#define _PR_ADD_TO_IOQ(_pq, _cpu) PR_APPEND_LINK(&_pq.links, &_PR_IOQ(_cpu))
421#define _PR_FD_READ_SET(_cpu)		((_cpu)->md.md_unix.fd_read_set)
422#define _PR_FD_READ_CNT(_cpu)		((_cpu)->md.md_unix.fd_read_cnt)
423#define _PR_FD_WRITE_SET(_cpu)		((_cpu)->md.md_unix.fd_write_set)
424#define _PR_FD_WRITE_CNT(_cpu)		((_cpu)->md.md_unix.fd_write_cnt)
425#define _PR_FD_EXCEPTION_SET(_cpu)	((_cpu)->md.md_unix.fd_exception_set)
426#define _PR_FD_EXCEPTION_CNT(_cpu)	((_cpu)->md.md_unix.fd_exception_cnt)
427#define _PR_IOQ_TIMEOUT(_cpu)		((_cpu)->md.md_unix.ioq_timeout)
428#define _PR_IOQ_MAX_OSFD(_cpu)		((_cpu)->md.md_unix.ioq_max_osfd)
429#define _PR_IOQ_OSFD_CNT(_cpu)		((_cpu)->md.md_unix.ioq_osfd_cnt)
430#define _PR_IOQ_POLLFDS(_cpu)		((_cpu)->md.md_unix.ioq_pollfds)
431#define _PR_IOQ_POLLFDS_SIZE(_cpu)	((_cpu)->md.md_unix.ioq_pollfds_size)
432
433#define _PR_IOQ_MIN_POLLFDS_SIZE(_cpu)	32
434
435struct _MDCPU {
436	struct _MDCPU_Unix md_unix;
437};
438
439#define _MD_INIT_LOCKS()
440#define _MD_NEW_LOCK(lock) PR_SUCCESS
441#define _MD_FREE_LOCK(lock)
442#define _MD_LOCK(lock)
443#define _MD_UNLOCK(lock)
444#define _MD_INIT_IO()
445#define _MD_IOQ_LOCK()
446#define _MD_IOQ_UNLOCK()
447
448extern PRStatus _MD_InitializeThread(PRThread *thread);
449
450#define _MD_INIT_RUNNING_CPU(cpu)       _MD_unix_init_running_cpu(cpu)
451#define _MD_INIT_THREAD                 _MD_InitializeThread
452#define _MD_EXIT_THREAD(thread)
453#define _MD_SUSPEND_THREAD(thread)      _MD_suspend_thread
454#define _MD_RESUME_THREAD(thread)       _MD_resume_thread
455#define _MD_CLEAN_THREAD(_thread)
456
457extern PRStatus _MD_CREATE_THREAD(
458    PRThread *thread,
459    void (*start) (void *),
460    PRThreadPriority priority,
461    PRThreadScope scope,
462    PRThreadState state,
463    PRUint32 stackSize);
464extern void _MD_SET_PRIORITY(struct _MDThread *thread, PRUintn newPri);
465extern PRStatus _MD_WAIT(PRThread *, PRIntervalTime timeout);
466extern PRStatus _MD_WAKEUP_WAITER(PRThread *);
467extern void _MD_YIELD(void);
468
469#endif /* ! _PR_PTHREADS */
470
471extern void _MD_EarlyInit(void);
472extern PRIntervalTime _PR_UNIX_GetInterval(void);
473extern PRIntervalTime _PR_UNIX_TicksPerSecond(void);
474
475#define _MD_EARLY_INIT                  _MD_EarlyInit
476#define _MD_FINAL_INIT					_PR_UnixInit
477#define _MD_GET_INTERVAL                _PR_UNIX_GetInterval
478#define _MD_INTERVAL_PER_SEC            _PR_UNIX_TicksPerSecond
479
480/*
481 * We wrapped the select() call.  _MD_SELECT refers to the built-in,
482 * unwrapped version.
483 */
484#define _MD_SELECT __select
485
486#ifdef _PR_POLL_AVAILABLE
487#include <sys/poll.h>
488extern int __syscall_poll(struct pollfd *ufds, unsigned long int nfds,
489	int timeout);
490#define _MD_POLL __syscall_poll
491#endif
492
493/* For writev() */
494#include <sys/uio.h>
495
496extern void _MD_linux_map_sendfile_error(int err);
497
498#endif /* nspr_linux_defs_h___ */
499