Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cygtls.h « cygwin « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 91533f2a0bf43ac9fd1585010b708f69ff1ea80b (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* cygtls.h

   Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013,
   2014 Red Hat, Inc.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#pragma once

#include <signal.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#define _NOMNTENT_FUNCS
#include <mntent.h>
#undef _NOMNTENT_FUNCS
#include <setjmp.h>

#define CYGTLS_INITIALIZED 0xc763173f

#ifndef CYG_MAX_PATH
# define CYG_MAX_PATH 260
#endif

#ifndef UNLEN
# define UNLEN 256
#endif

#define TLS_STACK_SIZE 256

#include "cygthread.h"

#define TP_NUM_C_BUFS 50
#define TP_NUM_W_BUFS 50

#ifdef CYGTLS_HANDLE
#include "thread.h"
#endif

#ifdef __x86_64__
#pragma pack(push,8)
#else
#pragma pack(push,4)
#endif

/* Defined here to support auto rebuild of tlsoffsets.h. */
class tls_pathbuf
{
  /* Make sure that c_cnt and w_cnt are always the first two members of this
     class, and never change the size (32 bit), unless you also change the
     mov statements in sigbe! */
  union
    {
      struct
	{
	  uint32_t c_cnt;
	  uint32_t w_cnt;
	};
      uint64_t _counters;
    };
  char  *c_buf[TP_NUM_C_BUFS];
  WCHAR *w_buf[TP_NUM_W_BUFS];

public:
  void destroy ();
  friend class tmp_pathbuf;
  friend class san;
};

class unionent
{
public:
  char *name;
  char **list;
  short port_proto_addrtype;
  short h_len;
  union
  {
    char *s_proto;
    char **h_addr_list;
  };
  enum struct_type
  {
    t_hostent, t_protoent, t_servent
  };
};

struct _local_storage
{
  /* passwd.cc */
  char pass[_PASSWORD_LEN];

  /* dlfcn.cc */
  int dl_error;
  char dl_buffer[256];

  /* path.cc */
  struct mntent mntbuf;
  int iteration;
  unsigned available_drives;
  char mnt_type[80];
  char mnt_opts[80];
  char mnt_fsname[CYG_MAX_PATH];
  char mnt_dir[CYG_MAX_PATH];

  /* select.cc */
  struct {
    HANDLE  sockevt;
    int     max_w4;
    LONG   *ser_num;			// note: malloced
    HANDLE *w4;				// note: malloced
  } select;

  /* strerror errno.cc */
  char strerror_buf[sizeof ("Unknown error -2147483648")];
  char strerror_r_buf[sizeof ("Unknown error -2147483648")];

  /* times.cc */
  char timezone_buf[20];

  /* strsig.cc */
  char signamebuf[sizeof ("Unknown signal 4294967295   ")];

  /* net.cc */
  char *ntoa_buf;			// note: malloced
  unionent *hostent_buf;		// note: malloced
  unionent *protoent_buf;		// note: malloced
  unionent *servent_buf;		// note: malloced

  /* cygthread.cc */
  char unknown_thread_name[30];

  /* syscalls.cc */
  int setmode_file;
  int setmode_mode;

  /* thread.cc */
  HANDLE cw_timer;

  tls_pathbuf pathbufs;
  char ttybuf[32];
};

typedef struct struct_waitq
{
  int pid;
  int options;
  int status;
  HANDLE ev;
  void *rusage;			/* pointer to potential rusage */
  struct struct_waitq *next;
  HANDLE thread_ev;
} waitq;

/* Changes to the below structure may require acompanying changes to the very
   simple parser in the perl script 'gentls_offsets' (<<-- start parsing here).
   The union in this structure is used to force alignment between the version
   of the compiler used to generate tlsoffsets.h and the cygwin cross compiler.
*/

/*gentls_offsets*/

extern "C" int __sjfault (jmp_buf);
extern "C" int __ljfault (jmp_buf, int);

/*gentls_offsets*/

typedef uintptr_t __stack_t;

class _cygtls
{
public:
  /* Please keep these two declarations first */
  struct _local_storage locals;
  union
  {
    struct _reent local_clib;
    char __dontuse[8 * ((sizeof(struct _reent) + 4) / 8)];
  };
  /**/
  void (*func) /*gentls_offsets*/(int, siginfo_t *, void *)/*gentls_offsets*/;
  int saved_errno;
  int sa_flags;
  sigset_t oldmask;
  sigset_t deltamask;
  int *errno_addr;
  sigset_t sigmask;
  sigset_t sigwait_mask;
  siginfo_t *sigwait_info;
  HANDLE signal_arrived;
  bool will_wait_for_signal;
  struct ucontext thread_context;
  DWORD thread_id;
  siginfo_t infodata;
  struct pthread *tid;
  class cygthread *_ctinfo;
  class san *andreas;
  waitq wq;
  int sig;
  unsigned incyg;
  unsigned spinning;
  unsigned stacklock;
  __stack_t *stackptr;
  __stack_t stack[TLS_STACK_SIZE];
  unsigned initialized;

  /*gentls_offsets*/
  void init_thread (void *, DWORD (*) (void *, void *));
  static void call (DWORD (*) (void *, void *), void *);
  void remove (DWORD);
  void push (__stack_t addr) {*stackptr++ = (__stack_t) addr;}
  __stack_t __reg1 pop ();
  __stack_t retaddr () {return stackptr[-1];}
  bool isinitialized () const
  {
    return initialized == CYGTLS_INITIALIZED;
  }
  bool __reg3 interrupt_now (CONTEXT *, siginfo_t&, void *, struct sigaction&);
  void __reg3 interrupt_setup (siginfo_t&, void *, struct sigaction&);

  bool inside_kernel (CONTEXT *);
  void __reg2 signal_debugger (siginfo_t&);

#ifdef CYGTLS_HANDLE
  operator HANDLE () const {return tid ? tid->win32_obj_id : NULL;}
#endif
  int __reg1 call_signal_handler ();
  void __reg1 remove_wq (DWORD);
  void __reg1 fixup_after_fork ();
  void __reg1 lock ();
  void __reg1 unlock ();
  bool __reg1 locked ();
  HANDLE get_signal_arrived (bool wait_for_lock = true)
  {
    if (!signal_arrived)
      {
	if (wait_for_lock)
	  lock ();
	if (!signal_arrived)
	  signal_arrived = CreateEvent (NULL, false, false, NULL);
	if (wait_for_lock)
	  unlock ();
      }
    return signal_arrived;
  }
  void set_signal_arrived (bool setit, HANDLE& h)
  {
    if (!setit)
      will_wait_for_signal = false;
    else
      {
	h = get_signal_arrived ();
	will_wait_for_signal = true;
      }
  }
  void reset_signal_arrived ()
  {
    if (signal_arrived)
      ResetEvent (signal_arrived);
    will_wait_for_signal = false;
  }
  void handle_SIGCONT ();
private:
  void __reg3 call2 (DWORD (*) (void *, void *), void *, void *);
  /*gentls_offsets*/
};
#pragma pack(pop)

/* FIXME: Find some way to autogenerate this value */
#ifdef __x86_64__
const int CYGTLS_PADSIZE = 12800;	/* Must be 16-byte aligned */
#else
const int CYGTLS_PADSIZE = 12700;
#endif

/*gentls_offsets*/

#include "cygerrno.h"
#include "ntdll.h"

#ifdef __x86_64__
/* When just using a "gs:X" asm for the x86_64 code, gcc wrongly creates
   pc-relative instructions.  However, NtCurrentTeb() is inline assembler
   anyway, so using it here should be fast enough on x86_64. */
#define _tlsbase (NtCurrentTeb()->Tib.StackBase)
#define _tlstop (NtCurrentTeb()->Tib.StackLimit)
#else
extern PVOID _tlsbase __asm__ ("%fs:4");
extern PVOID _tlstop __asm__ ("%fs:8");
#endif

#define _my_tls (*((_cygtls *) ((char *)_tlsbase - CYGTLS_PADSIZE)))
extern _cygtls *_main_tls;
extern _cygtls *_sig_tls;

#ifdef __x86_64__
class san
{
  uint64_t _cnt;
public:
  san () __attribute__ ((always_inline))
  {
    _cnt = _my_tls.locals.pathbufs._counters;
  }
  /* This is the first thing called in the __except handler.  The attribute
     "returns_twice" makes sure that GCC disregards any register value set
     earlier in the function, so this call serves as a register barrier. */
  void leave () __attribute__ ((returns_twice));
};
#else
class san
{
  san *_clemente;
  jmp_buf _context;
  uint32_t _c_cnt;
  uint32_t _w_cnt;
public:
  int setup () __attribute__ ((always_inline))
  {
    _clemente = _my_tls.andreas;
    _my_tls.andreas = this;
    _c_cnt = _my_tls.locals.pathbufs.c_cnt;
    _w_cnt = _my_tls.locals.pathbufs.w_cnt;
    return __sjfault (_context);
  }
  void leave () __attribute__ ((always_inline))
  {
    /* Restore tls_pathbuf counters in case of error. */
    _my_tls.locals.pathbufs.c_cnt = _c_cnt;
    _my_tls.locals.pathbufs.w_cnt = _w_cnt;
    __ljfault (_context, 1);
  }
  void reset () __attribute__ ((always_inline))
  {
    _my_tls.andreas = _clemente;
  }
};

class myfault
{
  san sebastian;
public:
  ~myfault () __attribute__ ((always_inline)) { sebastian.reset (); }
  inline int faulted () __attribute__ ((always_inline))
  {
    return sebastian.setup ();
  }
};
#endif

/* Exception handling macros.  These are required because SEH differs a lot
   between 32 and 64 bit.  Essentially, on 64 bit, we have to create compile
   time SEH tables which define the handler and try/except labels, while on
   32 bit we can simply set up an SJLJ handler within the myfault class. */
#define __mem_barrier	__asm__ __volatile__ ("" ::: "memory")
#ifdef __x86_64__
#define __try \
  { \
    __label__ __l_try, __l_except, __l_endtry; \
    __mem_barrier; \
    san __sebastian; \
    __asm__ goto ("\n" \
      "  .seh_handler _ZN9exception7myfaultEP17_EXCEPTION_RECORDPvP8_CONTEXTP19_DISPATCHER_CONTEXT, @except						\n" \
      "  .seh_handlerdata						\n" \
      "  .long 1							\n" \
      "  .rva %l[__l_try],%l[__l_endtry],%l[__l_except],%l[__l_except]	\n" \
      "  .seh_code							\n" \
      : : : : __l_try, __l_endtry, __l_except); \
    { \
      __l_try: \
	__mem_barrier;

#define __leave	\
      goto __l_endtry

#define __except(__errno) \
      goto __l_endtry; \
    } \
    { \
      __l_except: \
	__mem_barrier; \
	__sebastian.leave (); \
	if (__errno) \
	  set_errno (__errno);

#define __endtry \
    } \
    __l_endtry: \
      __mem_barrier; \
  }

#else /* !__x86_64__ */
#define __try \
  { \
    __label__ __l_endtry; \
    myfault efault; \
    if (!efault.faulted ()) \
      {

#define __leave	\
	goto __l_endtry

#define __except(__errno) \
	  goto __l_endtry; \
      } \
      { \
	if (__errno) \
	  set_errno (__errno);

#define __endtry \
      } \
    __l_endtry: \
      __mem_barrier; \
  }
#endif /* __x86_64__ */

class set_signal_arrived
{
public:
  set_signal_arrived (bool setit, HANDLE& h) { _my_tls.set_signal_arrived (setit, h); }
  set_signal_arrived (HANDLE& h) { _my_tls.set_signal_arrived (true, h); }

  operator int () const {return _my_tls.will_wait_for_signal;}
  ~set_signal_arrived () { _my_tls.reset_signal_arrived (); }
};

#define __getreent() (&_my_tls.local_clib)
/*gentls_offsets*/