This file is indexed.

/usr/share/systemtap/runtime/linux/loc2c-runtime.h is in systemtap-common 3.1-3ubuntu0.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/* target operations in the Linux kernel mode
 * Copyright (C) 2005-2016 Red Hat Inc.
 * Copyright (C) 2005-2007 Intel Corporation.
 * Copyright (C) 2007 Quentin Barnes.
 *
 * This file is part of systemtap, and is free software.  You can
 * redistribute it and/or modify it under the terms of the GNU General
 * Public License (GPL); either version 2, or (at your option) any
 * later version.
 */

#ifndef _LINUX_LOC2C_RUNTIME_H_
#define _LINUX_LOC2C_RUNTIME_H_

#ifdef STAPCONF_LINUX_UACCESS_H
#include <linux/uaccess.h>
#else
#include <asm/uaccess.h>
#endif
#include <linux/types.h>
#define intptr_t long
#define uintptr_t unsigned long

#include "../loc2c-runtime.h"


#ifndef STAPCONF_PAGEFAULT_DISABLE  /* before linux commit a866374a */
#define pagefault_disable() preempt_disable()
#define pagefault_enable() preempt_enable_no_resched()
#endif


#define k_fetch_register(regno) \
  pt_regs_fetch_register(c->kregs, regno)
#define k_store_register(regno, value) \
  pt_regs_store_register(c->kregs, regno, value)


/* PR 10601: user-space (user_regset) register access.
   Needs arch specific code, only i386 and x86_64 for now.  */
#if ((defined(STAPCONF_REGSET) || defined(STAPCONF_UTRACE_REGSET)) \
     && (defined (__i386__) || defined (__x86_64__)))

#if defined(STAPCONF_REGSET)
#include <linux/regset.h>
#endif

#if defined(STAPCONF_UTRACE_REGSET)
#include <linux/tracehook.h>
/* adapt new names to old decls */
#define user_regset_view utrace_regset_view
#define user_regset utrace_regset
#define task_user_regset_view utrace_native_view

#else // PR13489, inodes-uprobes export kludge
#if !defined(STAPCONF_TASK_USER_REGSET_VIEW_EXPORTED)
// First typedef from the original decl, then #define it as a typecasted call.
// NB: not all archs actually have the function, but the decl is universal in regset.h
typedef typeof(&task_user_regset_view) task_user_regset_view_fn;
/* Special macro to tolerate the kallsyms function pointer being zero. */
#define task_user_regset_view(t) (kallsyms_task_user_regset_view ? \
                                  (* (task_user_regset_view_fn)(kallsyms_task_user_regset_view))((t)) : \
                                  NULL)
#endif
#endif

struct usr_regset_lut {
  char *name;
  unsigned rsn;
  unsigned pos;
};


/* DWARF register number -to- user_regset bank/offset mapping table.
   The register numbers come from the processor-specific ELF documents.
   The user-regset bank/offset values come from kernel $ARCH/include/asm/user*.h
   or $ARCH/kernel/ptrace.c. */
#if defined (__i386__) || defined (__x86_64__)
static const struct usr_regset_lut url_i386[] = {
  { "ax", NT_PRSTATUS, 6*4 },
  { "cx", NT_PRSTATUS, 1*4 },
  { "dx", NT_PRSTATUS, 2*4 },
  { "bx", NT_PRSTATUS, 0*4 },
  { "sp", NT_PRSTATUS, 15*4 },
  { "bp", NT_PRSTATUS, 5*4 },
  { "si", NT_PRSTATUS, 3*4 },
  { "di", NT_PRSTATUS, 4*4 },
  { "ip", NT_PRSTATUS, 12*4 },
};
#endif

#if defined (__x86_64__)
static const struct usr_regset_lut url_x86_64[] = {
  { "rax", NT_PRSTATUS, 10*8 },
  { "rdx", NT_PRSTATUS, 12*8 },
  { "rcx", NT_PRSTATUS, 11*8 },
  { "rbx", NT_PRSTATUS, 5*8 },
  { "rsi", NT_PRSTATUS, 13*8 },
  { "rdi", NT_PRSTATUS, 14*8 },
  { "rbp", NT_PRSTATUS, 4*8 },
  { "rsp", NT_PRSTATUS, 19*8 },
  { "r8", NT_PRSTATUS, 9*8 },
  { "r9", NT_PRSTATUS, 8*8 }, 
  { "r10", NT_PRSTATUS, 7*8 },  
  { "r11", NT_PRSTATUS, 6*8 }, 
  { "r12", NT_PRSTATUS, 3*8 }, 
  { "r13", NT_PRSTATUS, 2*8 }, 
  { "r14", NT_PRSTATUS, 1*8 }, 
  { "r15", NT_PRSTATUS, 0*8 }, 
  { "rip", NT_PRSTATUS, 16*8 }, 
  /* XXX: SSE registers %xmm0-%xmm7 */ 
  /* XXX: SSE2 registers %xmm8-%xmm15 */
  /* XXX: FP registers %st0-%st7 */
  /* XXX: MMX registers %mm0-%mm7 */
};
#endif
/* XXX: insert other architectures here. */


static u32 ursl_fetch32 (const struct usr_regset_lut* lut, unsigned lutsize, int e_machine, unsigned regno)
{
  u32 value = ~0;
  const struct user_regset_view *rsv = task_user_regset_view(current); 
  unsigned rsi;
  int rc;
  unsigned rsn;
  unsigned pos;
  unsigned count;

  WARN_ON (!rsv);
  if (!rsv) goto out;
  WARN_ON (regno >= lutsize);
  if (regno >= lutsize) goto out;
  if (rsv->e_machine != e_machine) goto out;

  rsn = lut[regno].rsn;
  pos = lut[regno].pos;
  count = sizeof(value);

  for (rsi=0; rsi<rsv->n; rsi++)
    if (rsv->regsets[rsi].core_note_type == rsn)
      {
        const struct user_regset *rs = & rsv->regsets[rsi];
        rc = (rs->get)(current, rs, pos, count, & value, NULL);
        WARN_ON (rc);
        /* success */
        goto out;
      }
  WARN_ON (1); /* did not find appropriate regset! */
  
 out:
  return value;
}


static void ursl_store32 (const struct usr_regset_lut* lut,unsigned lutsize,  int e_machine, unsigned regno, u32 value)
{
  const struct user_regset_view *rsv = task_user_regset_view(current); 
  unsigned rsi;
  int rc;
  unsigned rsn;
  unsigned pos;
  unsigned count;

  WARN_ON (!rsv);
  if (!rsv) goto out;
  WARN_ON (regno >= lutsize);
  if (regno >= lutsize) goto out;
  if (rsv->e_machine != e_machine) goto out;

  rsn = lut[regno].rsn;
  pos = lut[regno].pos;
  count = sizeof(value);

  for (rsi=0; rsi<rsv->n; rsi++)
    if (rsv->regsets[rsi].core_note_type == rsn)
      {
        const struct user_regset *rs = & rsv->regsets[rsi];
        rc = (rs->set)(current, rs, pos, count, & value, NULL);
        WARN_ON (rc);
        /* success */
        goto out;
      }
  WARN_ON (1); /* did not find appropriate regset! */
  
 out:
  return;
}


static u64 ursl_fetch64 (const struct usr_regset_lut* lut, unsigned lutsize, int e_machine, unsigned regno)
{
  u64 value = ~0;
  const struct user_regset_view *rsv = task_user_regset_view(current); 
  unsigned rsi;
  int rc;
  unsigned rsn;
  unsigned pos;
  unsigned count;

  if (!rsv) goto out;
  if (regno >= lutsize) goto out;
  if (rsv->e_machine != e_machine) goto out;

  rsn = lut[regno].rsn;
  pos = lut[regno].pos;
  count = sizeof(value);

  for (rsi=0; rsi<rsv->n; rsi++)
    if (rsv->regsets[rsi].core_note_type == rsn)
      {
        const struct user_regset *rs = & rsv->regsets[rsi];
        rc = (rs->get)(current, rs, pos, count, & value, NULL);
        if (rc)
          goto out;
        /* success */
        return value;
      }
 out:
  printk (KERN_WARNING "process %d mach %d regno %d not available for fetch.\n", current->tgid, e_machine, regno);
  return value;
}


static void ursl_store64 (const struct usr_regset_lut* lut,unsigned lutsize,  int e_machine, unsigned regno, u64 value)
{
  const struct user_regset_view *rsv = task_user_regset_view(current); 
  unsigned rsi;
  int rc;
  unsigned rsn;
  unsigned pos;
  unsigned count;

  WARN_ON (!rsv);
  if (!rsv) goto out;
  WARN_ON (regno >= lutsize);
  if (regno >= lutsize) goto out;
  if (rsv->e_machine != e_machine) goto out;

  rsn = lut[regno].rsn;
  pos = lut[regno].pos;
  count = sizeof(value);

  for (rsi=0; rsi<rsv->n; rsi++)
    if (rsv->regsets[rsi].core_note_type == rsn)
      {
        const struct user_regset *rs = & rsv->regsets[rsi];
        rc = (rs->set)(current, rs, pos, count, & value, NULL);
        if (rc)
          goto out;
        /* success */
        return;
      }
  
 out:
  printk (KERN_WARNING "process %d mach %d regno %d not available for store.\n", current->tgid, e_machine, regno);
  return;
}


#if defined (__i386__)

#define u_fetch_register(regno) ursl_fetch32(url_i386, ARRAY_SIZE(url_i386), EM_386, regno)
#define u_store_register(regno,value) ursl_store32(url_i386, ARRAY_SIZE(url_i386), EM_386, regno, value)

#elif defined (__x86_64__)

#define u_fetch_register(regno) (_stp_is_compat_task() ? ursl_fetch32(url_i386, ARRAY_SIZE(url_i386), EM_386, regno) : ursl_fetch64(url_x86_64, ARRAY_SIZE(url_x86_64), EM_X86_64, regno))
#define u_store_register(regno,value)  (_stp_is_compat_task() ? ursl_store32(url_i386, ARRAY_SIZE(url_i386), EM_386, regno, value) : ursl_store64(url_x86_64, ARRAY_SIZE(url_x86_64), EM_X86_64, regno, value))

#endif

#else /* ! STAPCONF_REGSET */

/* Downgrade to pt_dwarf_register access. */

#define u_store_register(regno, value) \
  pt_regs_store_register(c->uregs, regno, value)

/* If we're in a 32/31-bit task in a 64-bit kernel, we need to emulate
 * 32-bitness by masking the output of pt_regs_fetch_register() */
#ifndef CONFIG_COMPAT
#define u_fetch_register(regno) \
  pt_regs_fetch_register(c->uregs, regno)
#else
#define u_fetch_register(regno) \
  (_stp_is_compat_task() ? (0xffffffff & pt_regs_fetch_register(c->uregs, regno)) \
                         : pt_regs_fetch_register(c->uregs, regno))
#endif

#endif /* STAPCONF_REGSET */


/* The deref and store_deref macros are called to safely access
   addresses in the probe context.  These macros are used only for
   kernel addresses.  The macros must handle bogus addresses here
   gracefully (as from corrupted data structures, stale pointers,
   etc), by doing a "goto deref_fault".

   On most machines, the asm/uaccess.h macros __get_user and
   __put_user macros do exactly the low-level work we need to access
   memory with fault handling, and are not actually specific to
   user-address access at all.  */

/*
 * On most platforms, __get_user_inatomic() and __put_user_inatomic()
 * are defined, which are the same as __get_user() and __put_user(),
 * but without a call to might_sleep(). Since we're disabling page
 * faults when we read, we want to use the 'inatomic' variants when
 * available.
 */
#ifdef __get_user_inatomic
#define __stp_get_user __get_user_inatomic
#else
#define __stp_get_user __get_user
#endif
#ifdef __put_user_inatomic
#define __stp_put_user __put_user_inatomic
#else
#define __stp_put_user __put_user
#endif

/*
 * Some arches (like aarch64) don't have __get_user_bad() or
 * __put_user_bad(), so use BUILD_BUG() instead.
 */
#ifdef BUILD_BUG
#define __stp_get_user_bad BUILD_BUG
#define __stp_put_user_bad BUILD_BUG
#else
#define __stp_get_user_bad __get_user_bad
#define __stp_put_user_bad __put_user_bad
#endif


/* 
 * __stp_deref_nocheck(): reads a simple type from a
 * location with no address sanity checking.
 *
 * value: read the simple type into this variable
 * size: number of bytes to read
 * addr: address to read from
 *
 * Note that the caller *must* check the address for validity and do
 * any other checks necessary. This macro is designed to be used as
 * the base for the other macros more suitable for the rest of the
 * code to use. Note that the caller is also responsible for ensuring
 * that the kernel doesn't pagefault while reading.
 */

#define __stp_deref_nocheck(value, size, addr)				      \
  ({									      \
    int _err = -EFAULT;							      \
    switch (size)							      \
      {									      \
      case 1: { u8 _b; _err = __stp_get_user(_b, ((u8 *)(uintptr_t)(addr)));  \
		(value) = _b; }						      \
	break;								      \
      case 2: { u16 _w; _err = __stp_get_user(_w, ((u16 *)(uintptr_t)(addr))); \
		(value) = _w; }						      \
	break;								      \
      case 4: { u32 _l; _err = __stp_get_user(_l, ((u32 *)(uintptr_t)(addr))); \
		(value) = _l; }						      \
	break;								      \
      case 8: { u64 _q; _err = __stp_get_user(_q, ((u64 *)(uintptr_t)(addr))); \
		(value) = _q; }						      \
	break;								      \
      default:								      \
 	__stp_get_user_bad();					      	      \
      }									      \
    _err;								      \
  })


/* 
 * _stp_deref(): safely read a simple type from memory
 *
 * size: number of bytes to read
 * addr: address to read from
 * seg: memory segment to use, either kernel (KERN_DS) or user
 * (USER_DS)
 * 
 * The macro returns the value read. If this macro gets an error while
 * trying to read memory, a DEREF_FAULT will occur. Note that the
 * kernel will not pagefault when trying to read the memory.
 */

#define _stp_deref(size, addr, seg)                                           \
  ({									      \
    int _bad = 0;							      \
    intptr_t _v = 0;							      \
    mm_segment_t _oldfs = get_fs();                                           \
    set_fs(seg);                                                              \
    pagefault_disable();                                                      \
    if (lookup_bad_addr(VERIFY_READ, (const unsigned long)(addr), (size)))    \
      _bad = -EFAULT;                                                         \
    else                                                                      \
      _bad = __stp_deref_nocheck(_v, (size), (addr));			      \
    pagefault_enable();                                                       \
    set_fs(_oldfs);                                                           \
    if (_bad)                                                                 \
      DEREF_FAULT(addr);						      \
    _v;									      \
  })


/* 
 * _stp_deref_nofault(): safely read a simple type from memory without
 * a DEREF_FAULT on error
 *
 * value: read the simple type into this variable
 * size: number of bytes to read
 * addr: address to read from
 * seg: memory segment to use, either kernel (KERN_DS) or user
 * (USER_DS)
 * 
 * If this macro gets an error while trying to read memory, nonzero is
 * returned. On success, 0 is return. Note that the kernel will not
 * pagefault when trying to read the memory.
 */

#define _stp_deref_nofault(value, size, addr, seg)			      \
  ({									      \
    int _bad = 0;							      \
    intptr_t _v = 0;							      \
    mm_segment_t _oldfs = get_fs();                                           \
    set_fs(seg);                                                              \
    pagefault_disable();                                                      \
    if (lookup_bad_addr(VERIFY_READ, (const unsigned long)(addr), (size)))    \
      _bad = -EFAULT;							      \
    else                                                                      \
      _bad = __stp_deref_nocheck((value), (size), (addr));		      \
    pagefault_enable();                                                       \
    set_fs(_oldfs);                                                           \
    _bad;								      \
  })


/* 
 * __stp_store_deref_nocheck(): writes a simple type to a location
 * with no address sanity checking.
 *
 * size: number of bytes to write
 * addr: address to write to
 * value: read the simple type from this variable
 *
 * Note that the caller *must* check the address for validity and do
 * any other checks necessary. This macro is designed to be used as
 * the base for the other macros more suitable for the rest of the
 * code to use. Note that the caller is also responsible for ensuring
 * that the kernel doesn't pagefault while writing.
 */

#define __stp_store_deref_nocheck(size, addr, value)			      \
  ({									      \
    int _err = -EFAULT;							      \
    switch (size)							      \
      {									      \
      case 1: _err = __stp_put_user(((u8)(value)), ((u8 *)(uintptr_t)(addr)));\
        break;								      \
      case 2: _err = __stp_put_user(((u16)(value)), ((u16 *)(uintptr_t)(addr)));\
        break;								      \
      case 4: _err = __stp_put_user(((u32)(value)), ((u32 *)(uintptr_t)(addr)));\
        break;								      \
      case 8: _err = __stp_put_user(((u64)(value)), ((u64 *)(uintptr_t)(addr)));\
        break;								      \
      default:								      \
	__stp_put_user_bad();						      \
      }									      \
    _err;								      \
  })


/* 
 * _stp_store_deref(): safely write a simple type to memory
 *
 * size: number of bytes to write
 * addr: address to write to
 * value: read the simple type from this variable
 * seg: memory segment to use, either kernel (KERN_DS) or user
 * (USER_DS)
 * 
 * The macro has no return value. If this macro gets an error while
 * trying to write, a STORE_DEREF_FAULT will occur. Note that the
 * kernel will not pagefault when trying to write the memory.
 */

#define _stp_store_deref(size, addr, value, seg)                              \
  ({									      \
    int _bad = 0;							      \
    mm_segment_t _oldfs = get_fs();                                           \
    set_fs(seg);                                                              \
    pagefault_disable();                                                      \
    if (lookup_bad_addr(VERIFY_WRITE, (const unsigned long)(addr), (size)))   \
      _bad = -EFAULT;                                                         \
    else                                                                      \
      _bad = __stp_store_deref_nocheck((size), (addr), (value));	      \
    pagefault_enable();                                                       \
    set_fs(_oldfs);                                                           \
    if (_bad)								      \
      STORE_DEREF_FAULT(addr);						      \
  })


/* Map kderef/uderef to the generic segment-aware deref macros. */ 

#ifndef kderef
#define kderef(s,a) _stp_deref(s,a,KERNEL_DS)
#endif

#ifndef store_kderef
#define store_kderef(s,a,v) _stp_store_deref(s,a,v,KERNEL_DS)
#endif

#ifndef uderef
#define uderef(s,a) _stp_deref(s,a,USER_DS)
#endif

#ifndef store_uderef
#define store_uderef(s,a,v) _stp_store_deref(s,a,v,USER_DS)
#endif

#if defined (__i386__) || defined (__arm__)

/* x86 and arm can't do 8-byte put/get_user_asm, so we have to split it */

#define __Xread(ptr, Xderef)				\
  ((sizeof(*(ptr)) == 8) ?				\
       *(typeof(ptr))&(u32[2]) {			\
	 (u32) Xderef(4, &((u32 *)(ptr))[0]),		\
	 (u32) Xderef(4, &((u32 *)(ptr))[1]) }		\
     : (typeof(*(ptr))) Xderef(sizeof(*(ptr)), (ptr)))

#define __Xwrite(ptr, value, store_Xderef)				     \
  ({									     \
    if (sizeof(*(ptr)) == 8) {						     \
      union { typeof(*(ptr)) v; u32 l[2]; } _kw;			     \
      _kw.v = (typeof(*(ptr)))(value);					     \
      store_Xderef(4, &((u32 *)(ptr))[0], _kw.l[0]);			     \
      store_Xderef(4, &((u32 *)(ptr))[1], _kw.l[1]);			     \
    } else								     \
      store_Xderef(sizeof(*(ptr)), (ptr), (long)(typeof(*(ptr)))(value));    \
  })

#else

#define __Xread(ptr, Xderef) \
  ( (typeof(*(ptr))) Xderef(sizeof(*(ptr)), (ptr)) )
#define __Xwrite(ptr, value, store_Xderef) \
  ( store_Xderef(sizeof(*(ptr)), (ptr), (long)(typeof(*(ptr)))(value)) )

#endif

#define kread(ptr) __Xread((ptr), kderef)
#define uread(ptr) __Xread((ptr), uderef)

#define kwrite(ptr, value) __Xwrite((ptr), (value), store_kderef)
#define uwrite(ptr, value) __Xwrite((ptr), (value), store_uderef)


/* Dereference a kernel buffer ADDR of size MAXBYTES. Put the bytes in
 * address DST (which can be NULL).
 *
 * This function is useful for reading memory when the size isn't a
 * size that kderef() handles.  This function is very similar to
 * kderef_string(), but kderef_buffer() doesn't quit when finding a
 * '\0' byte or append a '\0' byte.
 */

#define kderef_buffer(dst, addr, maxbytes)				      \
  ({									      \
    uintptr_t _addr;							      \
    size_t _len;							      \
    unsigned char _c;							      \
    char *_d = (dst);							      \
    int _bad = 0;							      \
    mm_segment_t _oldfs = get_fs();                                           \
    set_fs(KERNEL_DS);							      \
    pagefault_disable();                                                      \
    if (lookup_bad_addr(VERIFY_READ, (const unsigned long)(addr), (maxbytes))) \
      _bad = 1;                                                               \
    else                                                                      \
      for (_len = (maxbytes), _addr = (uintptr_t)(addr);		      \
	   _len > 1; --_len, ++_addr)					      \
        {								      \
	  _bad = __stp_deref_nocheck(_c, 1, _addr);			      \
	  if (_bad)							      \
	    break;							      \
	  else if (_d)							      \
	    *_d++ = _c;							      \
	}								      \
    pagefault_enable();                                                       \
    set_fs(_oldfs);                                                           \
    if (_bad)                                                                 \
      DEREF_FAULT(addr);						      \
    (dst);								      \
  })

/* The following is for kernel strings, see the uconversions.stp
   tapset for user_string functions. */


#define kderef_string(dst, addr, maxbytes)				      \
  ({									      \
    uintptr_t _addr;							      \
    size_t _len;							      \
    unsigned char _c;							      \
    char *_d = (dst);							      \
    int _bad = 0;							      \
    mm_segment_t _oldfs = get_fs();                                           \
    set_fs(KERNEL_DS);							      \
    pagefault_disable();                                                      \
    if (lookup_bad_addr(VERIFY_READ, (const unsigned long)(addr), (maxbytes))) \
      _bad = 1;                                                               \
    else                                                                      \
      {									      \
	for (_len = (maxbytes), _addr = (uintptr_t)(addr);		      \
	     _len > 1; --_len, ++_addr)					      \
	  {								      \
	    _bad = __stp_deref_nocheck(_c, 1, _addr);			      \
	    if (_bad || _c == '\0')					      \
	      break;							      \
	    else if (_d)						      \
	      *_d++ = _c;						      \
	  }								      \
	if (!_bad && _d)						      \
	  *_d = '\0';							      \
      }									      \
    pagefault_enable();                                                       \
    set_fs(_oldfs);                                                           \
    if (_bad)                                                                 \
      DEREF_FAULT(addr);						      \
    (dst);								      \
  })


/* 
 * _stp_store_deref_string(): safely write a string to memory
 *
 * src: source string
 * addr: address to write to
 * maxbytes: maximum number of bytes to write
 * seg: memory segment to use, either kernel (KERN_DS) or user
 * (USER_DS)
 * 
 * The macro has no return value. If this macro gets an error while
 * trying to write, a STORE_DEREF_FAULT will occur. Note that the
 * kernel will not pagefault when trying to write the memory.
 */

#define _stp_store_deref_string(src, addr, maxbytes, seg)		      \
  ({									      \
    uintptr_t _addr;							      \
    size_t _len;							      \
    char *_s = (src);							      \
    int _bad = 0;							      \
    mm_segment_t _oldfs = get_fs();                                           \
    set_fs(seg);                                                              \
    pagefault_disable();                                                      \
    if (lookup_bad_addr(VERIFY_WRITE, (const unsigned long)(addr), (maxbytes))) \
      _bad = 1;                                                               \
    else                                                                      \
      {									      \
	for (_len = (maxbytes), _addr = (uintptr_t)(addr);		      \
	     _len > 1 && _s && *_s != '\0'; --_len, ++_addr)		      \
	  {								      \
	    _bad = __stp_store_deref_nocheck(1, _addr, *_s++);		      \
	    if (_bad)					      		      \
	      break;							      \
	  }								      \
	if (!_bad)							      \
	  __stp_store_deref_nocheck(1, _addr, '\0');			      \
      }									      \
    pagefault_enable();                                                       \
    set_fs(_oldfs);                                                           \
    if (_bad)								      \
      STORE_DEREF_FAULT(addr);						      \
  })


/* 
 * store_kderef_string(): safely write a string to kernel memory
 *
 * src: source string
 * addr: address to write to
 * maxbytes: maximum number of bytes to write
 * 
 * The macro has no return value. If this macro gets an error while
 * trying to write, a STORE_DEREF_FAULT will occur. Note that the
 * kernel will not pagefault when trying to write the memory.
 */

#define store_kderef_string(src, addr, maxbytes)			      \
  _stp_store_deref_string((src), (addr), (maxbytes), KERNEL_DS)


/* 
 * store_uderef_string(): safely write a string to user memory
 *
 * src: source string
 * addr: address to write to
 * maxbytes: maximum number of bytes to write
 * 
 * The macro has no return value. If this macro gets an error while
 * trying to write, a STORE_DEREF_FAULT will occur. Note that the
 * kernel will not pagefault when trying to write the memory.
 */

#define store_uderef_string(src, addr, maxbytes)			      \
  _stp_store_deref_string((src), (addr), (maxbytes), USER_DS)

#endif /* _LINUX_LOC2C_RUNTIME_H_ */