Release 940804
[wine/gsoc-2012-control.git] / loader / ldtlib.c
blob40929d654db021bf6b7827d906a95d5703e970fc
1 #ifndef WINELIB
2 static char RCSId[] = "$Id: ldtlib.c,v 1.2 1993/07/04 04:04:21 root Exp root $";
3 static char Copyright[] = "Copyright Robert J. Amstadt, 1993";
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <errno.h>
8 #ifdef linux
9 #include <linux/unistd.h>
10 #include <linux/head.h>
11 #include <linux/ldt.h>
13 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
14 #endif
15 #if defined(__NetBSD__) || defined(__FreeBSD__)
16 #include <machine/segments.h>
18 extern int i386_get_ldt(int, union descriptor *, int);
19 extern int i386_set_ldt(int, union descriptor *, int);
21 struct segment_descriptor *
22 make_sd(unsigned base, unsigned limit, int contents, int read_exec_only, int seg32, int inpgs)
24 static long d[2];
26 d[0] = ((base & 0x0000ffff) << 16) |
27 (limit & 0x0ffff);
28 d[1] = (base & 0xff000000) |
29 ((base & 0x00ff0000)>>16) |
30 (limit & 0xf0000) |
31 (contents << 10) |
32 ((read_exec_only ^ 1) << 9) |
33 (seg32 << 22) |
34 (inpgs << 23) |
35 0xf000;
37 return ((struct segment_descriptor *)d);
39 #endif
41 int
42 get_ldt(void *buffer)
44 #ifdef linux
45 return modify_ldt(0, buffer, 32 * sizeof(struct modify_ldt_ldt_s));
46 #endif
47 #if defined(__NetBSD__) || defined(__FreeBSD__)
48 return i386_get_ldt(0, (union descriptor *)buffer, 32);
49 #endif
52 int
53 set_ldt_entry(int entry, unsigned long base, unsigned int limit,
54 int seg_32bit_flag, int contents, int read_only_flag,
55 int limit_in_pages_flag)
57 #ifdef linux
58 struct modify_ldt_ldt_s ldt_info;
60 ldt_info.entry_number = entry;
61 ldt_info.base_addr = base;
62 ldt_info.limit = limit;
63 ldt_info.seg_32bit = seg_32bit_flag;
64 ldt_info.contents = contents;
65 ldt_info.read_exec_only = read_only_flag;
66 ldt_info.limit_in_pages = limit_in_pages_flag;
67 #ifdef NEW_LDT_STRUCT
68 ldt_info.seg_not_present = 0;
69 #endif
71 return modify_ldt(1, &ldt_info, sizeof(ldt_info));
72 #endif
73 #if defined(__NetBSD__) || defined(__FreeBSD__)
74 struct segment_descriptor *sd;
75 int ret;
77 #ifdef DEBUG_LDT
78 printf("set_ldt_entry: entry=%x base=%x limit=%x%s %s-bit contents=%d %s\n",
79 entry, base, limit, limit_in_pages_flag?"-pages":"",
80 seg_32bit_flag?"32":"16",
81 contents, read_only_flag?"read-only":"");
82 #endif
84 sd = make_sd(base, limit, contents, read_only_flag, seg_32bit_flag, limit_in_pages_flag);
85 ret = i386_set_ldt(entry, (union descriptor *)sd, 1);
86 if (ret < 0) {
87 perror("i386_set_ldt");
88 fprintf(stderr,
89 "Did you reconfigure the kernel with \"options USER_LDT\"?\n");
90 exit(1);
93 return ret;
95 #endif
97 #endif /* ifndef WINELIB */