Release 940815
[wine/gsoc-2012-control.git] / loader / ldtlib.c
blob181c4b1270835306ff878ad27ad84e35686f0b22
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"
10 #ifdef linux
11 #include <linux/unistd.h>
12 #include <linux/head.h>
13 #include <linux/ldt.h>
15 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
16 #endif
17 #if defined(__NetBSD__) || defined(__FreeBSD__)
18 #include <machine/segments.h>
20 extern int i386_get_ldt(int, union descriptor *, int);
21 extern int i386_set_ldt(int, union descriptor *, int);
23 struct segment_descriptor *
24 make_sd(unsigned base, unsigned limit, int contents, int read_exec_only, int seg32, int inpgs)
26 static long d[2];
28 d[0] = ((base & 0x0000ffff) << 16) |
29 (limit & 0x0ffff);
30 d[1] = (base & 0xff000000) |
31 ((base & 0x00ff0000)>>16) |
32 (limit & 0xf0000) |
33 (contents << 10) |
34 ((read_exec_only ^ 1) << 9) |
35 (seg32 << 22) |
36 (inpgs << 23) |
37 0xf000;
39 return ((struct segment_descriptor *)d);
41 #endif
43 int
44 get_ldt(void *buffer)
46 #ifdef linux
47 return modify_ldt(0, buffer, 32 * sizeof(struct modify_ldt_ldt_s));
48 #endif
49 #if defined(__NetBSD__) || defined(__FreeBSD__)
50 return i386_get_ldt(0, (union descriptor *)buffer, 32);
51 #endif
54 int
55 set_ldt_entry(int entry, unsigned long base, unsigned int limit,
56 int seg_32bit_flag, int contents, int read_only_flag,
57 int limit_in_pages_flag)
59 #ifdef linux
60 struct modify_ldt_ldt_s ldt_info;
62 ldt_info.entry_number = entry;
63 ldt_info.base_addr = base;
64 ldt_info.limit = limit;
65 ldt_info.seg_32bit = seg_32bit_flag;
66 ldt_info.contents = contents;
67 ldt_info.read_exec_only = read_only_flag;
68 ldt_info.limit_in_pages = limit_in_pages_flag;
69 #ifdef NEW_LDT_STRUCT
70 ldt_info.seg_not_present = 0;
71 #endif
73 return modify_ldt(1, &ldt_info, sizeof(ldt_info));
74 #endif
75 #if defined(__NetBSD__) || defined(__FreeBSD__)
76 struct segment_descriptor *sd;
77 int ret;
79 #ifdef DEBUG_LDT
80 printf("set_ldt_entry: entry=%x base=%x limit=%x%s %s-bit contents=%d %s\n",
81 entry, base, limit, limit_in_pages_flag?"-pages":"",
82 seg_32bit_flag?"32":"16",
83 contents, read_only_flag?"read-only":"");
84 #endif
86 sd = make_sd(base, limit, contents, read_only_flag, seg_32bit_flag, limit_in_pages_flag);
87 ret = i386_set_ldt(entry, (union descriptor *)sd, 1);
88 if (ret < 0) {
89 perror("i386_set_ldt");
90 fprintf(stderr,
91 "Did you reconfigure the kernel with \"options USER_LDT\"?\n");
92 exit(1);
95 return ret;
97 #endif
99 #endif /* ifndef WINELIB */