Release 941017
[wine/gsoc-2012-control.git] / loader / ldtlib.c
blob8266c7f583598e572eb6c703a3279a1f1cbd05e0
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 #include "autoconf.h"
9 #include "stddebug.h"
10 /* #define DEBUG_LDT /* */
11 /* #undef DEBUG_LDT /* */
12 #include "debug.h"
14 #ifdef linux
15 #include <linux/unistd.h>
16 #include <linux/head.h>
17 #include <linux/ldt.h>
19 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
20 #endif
21 #if defined(__NetBSD__) || defined(__FreeBSD__)
22 #include <machine/segments.h>
24 extern int i386_get_ldt(int, union descriptor *, int);
25 extern int i386_set_ldt(int, union descriptor *, int);
27 struct segment_descriptor *
28 make_sd(unsigned base, unsigned limit, int contents, int read_exec_only, int seg32, int inpgs)
30 static long d[2];
32 d[0] = ((base & 0x0000ffff) << 16) |
33 (limit & 0x0ffff);
34 d[1] = (base & 0xff000000) |
35 ((base & 0x00ff0000)>>16) |
36 (limit & 0xf0000) |
37 (contents << 10) |
38 ((read_exec_only ^ 1) << 9) |
39 (seg32 << 22) |
40 (inpgs << 23) |
41 0xf000;
43 return ((struct segment_descriptor *)d);
45 #endif
47 int
48 get_ldt(void *buffer)
50 #ifdef linux
51 return modify_ldt(0, buffer, 32 * sizeof(struct modify_ldt_ldt_s));
52 #endif
53 #if defined(__NetBSD__) || defined(__FreeBSD__)
54 return i386_get_ldt(0, (union descriptor *)buffer, 32);
55 #endif
58 int
59 set_ldt_entry(int entry, unsigned long base, unsigned int limit,
60 int seg_32bit_flag, int contents, int read_only_flag,
61 int limit_in_pages_flag)
63 #ifdef linux
64 struct modify_ldt_ldt_s ldt_info;
66 ldt_info.entry_number = entry;
67 ldt_info.base_addr = base;
68 ldt_info.limit = limit;
69 ldt_info.seg_32bit = seg_32bit_flag;
70 ldt_info.contents = contents;
71 ldt_info.read_exec_only = read_only_flag;
72 ldt_info.limit_in_pages = limit_in_pages_flag;
73 #ifdef NEW_LDT_STRUCT
74 ldt_info.seg_not_present = 0;
75 #endif
77 return modify_ldt(1, &ldt_info, sizeof(ldt_info));
78 #endif
79 #if defined(__NetBSD__) || defined(__FreeBSD__)
80 struct segment_descriptor *sd;
81 int ret;
83 dprintf_ldt(stddeb,
84 "set_ldt_entry: entry=%x base=%x limit=%x%s %s-bit contents=%d %s\n",
85 entry, base, limit, limit_in_pages_flag?"-pages":"",
86 seg_32bit_flag?"32":"16",
87 contents, read_only_flag?"read-only":"");
89 sd = make_sd(base, limit, contents, read_only_flag, seg_32bit_flag, limit_in_pages_flag);
90 ret = i386_set_ldt(entry, (union descriptor *)sd, 1);
91 if (ret < 0) {
92 perror("i386_set_ldt");
93 fprintf(stderr,
94 "Did you reconfigure the kernel with \"options USER_LDT\"?\n");
95 exit(1);
98 return ret;
100 #endif
102 #endif /* ifndef WINELIB */