2 * LDT manipulation functions
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
17 #include <linux/unistd.h>
18 #include <linux/head.h>
19 #include <linux/ldt.h>
21 _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
24 #if defined(__svr4__) || defined(_SCO_DS)
25 #include <sys/sysi86.h>
29 #if defined(__NetBSD__) || defined(__FreeBSD__)
30 #include <machine/segments.h>
32 extern int i386_get_ldt(int, union descriptor *, int);
33 extern int i386_set_ldt(int, union descriptor *, int);
34 #endif /* __NetBSD__ || __FreeBSD__ */
37 ldt_copy_entry ldt_copy[LDT_SIZE];
38 unsigned char ldt_flags_copy[LDT_SIZE];
41 /***********************************************************************
44 * Convert the raw bytes of the descriptor to an ldt_entry structure.
46 void LDT_BytesToEntry( const unsigned long *buffer, ldt_entry *content )
48 content->base = (*buffer >> 16) & 0x0000ffff;
49 content->limit = *buffer & 0x0000ffff;
51 content->base |= (*buffer & 0xff000000) | ((*buffer << 16) & 0x00ff0000);
52 content->limit |= (*buffer & 0x000f0000);
53 content->type = (*buffer >> 10) & 3;
54 content->seg_32bit = (*buffer & 0x00400000) != 0;
55 content->read_only = (*buffer & 0x00000200) == 0;
56 content->limit_in_pages = (*buffer & 0x00800000) != 0;
60 /***********************************************************************
63 * Convert an ldt_entry structure to the raw bytes of the descriptor.
65 void LDT_EntryToBytes( unsigned long *buffer, const ldt_entry *content )
67 *buffer++ = ((content->base & 0x0000ffff) << 16) |
68 (content->limit & 0x0ffff);
69 *buffer = (content->base & 0xff000000) |
70 ((content->base & 0x00ff0000)>>16) |
71 (content->limit & 0xf0000) |
72 (content->type << 10) |
73 ((content->read_only == 0) << 9) |
74 ((content->seg_32bit != 0) << 22) |
75 ((content->limit_in_pages != 0) << 23) |
80 /***********************************************************************
83 * Retrieve an LDT entry.
85 int LDT_GetEntry( int entry, ldt_entry *content )
89 content->base = ldt_copy[entry].base;
90 content->limit = ldt_copy[entry].limit;
91 content->type = (ldt_flags_copy[entry] & LDT_FLAGS_TYPE);
92 content->seg_32bit = (ldt_flags_copy[entry] & LDT_FLAGS_32BIT) != 0;
93 content->read_only = (ldt_flags_copy[entry] & LDT_FLAGS_READONLY) !=0;
94 content->limit_in_pages = (ldt_flags_copy[entry] & LDT_FLAGS_BIG) !=0;
95 if (content->limit_in_pages) content->limit >>= 12;
100 /***********************************************************************
105 int LDT_SetEntry( int entry, const ldt_entry *content )
110 "LDT_SetEntry: entry=%04x base=%08lx limit=%05lx %s %d-bit flags=%c%c%c\n",
111 entry, content->base, content->limit,
112 content->limit_in_pages ? "pages" : "bytes",
113 content->seg_32bit ? 32 : 16,
114 content->read_only && (content->type & SEGMENT_CODE) ? '-' : 'r',
115 content->read_only || (content->type & SEGMENT_CODE) ? '-' : 'w',
116 (content->type & SEGMENT_CODE) ? 'x' : '-' );
118 /* Entry 0 must not be modified; its base and limit are always 0 */
119 if (!entry) return 0;
124 struct modify_ldt_ldt_s ldt_info;
126 /* Clear all unused bits (like seg_not_present) */
127 memset( &ldt_info, 0, sizeof(ldt_info) );
128 ldt_info.entry_number = entry;
129 ldt_info.base_addr = content->base;
130 ldt_info.limit = content->limit;
131 ldt_info.seg_32bit = content->seg_32bit != 0;
132 ldt_info.contents = content->type;
133 ldt_info.read_exec_only = content->read_only != 0;
134 ldt_info.limit_in_pages = content->limit_in_pages != 0;
135 /* Make sure the info will be accepted by the kernel */
136 /* This is ugly, but what can I do? */
137 if (content->type == SEGMENT_STACK)
143 if (ldt_info.base_addr >= 0xc0000000)
145 fprintf( stderr, "LDT_SetEntry: invalid base addr %08lx\n",
146 ldt_info.base_addr );
149 if (content->limit_in_pages)
151 if ((ldt_info.limit << 12) + 0xfff >
152 0xc0000000 - ldt_info.base_addr)
153 ldt_info.limit = (0xc0000000 - 0xfff - ldt_info.base_addr) >> 12;
157 if (ldt_info.limit > 0xc0000000 - ldt_info.base_addr)
158 ldt_info.limit = 0xc0000000 - ldt_info.base_addr;
161 if ((ret = modify_ldt(1, &ldt_info, sizeof(ldt_info))) < 0)
162 perror( "modify_ldt" );
166 #if defined(__NetBSD__) || defined(__FreeBSD__)
171 LDT_EntryToBytes( d, content );
172 ret = i386_set_ldt(entry, (union descriptor *)d, 1);
175 perror("i386_set_ldt");
177 "Did you reconfigure the kernel with \"options USER_LDT\"?\n");
181 #endif /* __NetBSD__ || __FreeBSD__ */
182 #if defined(__svr4__) || defined(_SCO_DS)
187 ldt_mod.sel = ENTRY_TO_SELECTOR(entry) | 4;
188 ldt_mod.bo = content->base;
189 ldt_mod.ls = content->limit;
190 i = ((content->limit & 0xf0000) |
191 (content->type << 10) |
192 (((content->read_only != 0) ^ 1) << 9) |
193 ((content->seg_32bit != 0) << 22) |
194 ((content->limit_in_pages != 0)<< 23) |
198 ldt_mod.acc1 = (i & 0xff00) >> 8;
199 ldt_mod.acc2 = (i & 0xf00000) >> 20;
201 if (content->base == 0)
206 if ((ret = sysi86(SI86DSCR, &ldt_mod)) == -1) perror("sysi86");
210 if (ret < 0) return ret;
211 ldt_copy[entry].base = content->base;
212 if (!content->limit_in_pages) ldt_copy[entry].limit = content->limit;
213 else ldt_copy[entry].limit = (content->limit << 12) | 0x0fff;
214 ldt_flags_copy[entry] = (content->type & LDT_FLAGS_TYPE) |
215 (content->read_only ? LDT_FLAGS_READONLY : 0) |
216 (content->seg_32bit ? LDT_FLAGS_32BIT : 0) |
217 (content->limit_in_pages ? LDT_FLAGS_BIG : 0) |
218 (ldt_flags_copy[entry] & LDT_FLAGS_ALLOCATED);
223 /***********************************************************************
226 * Print the content of the LDT on stdout.
228 void LDT_Print( int start, int length )
233 if (length == -1) length = LDT_SIZE - start;
234 for (i = start; i < start + length; i++)
236 if (!ldt_copy[i].base && !ldt_copy[i].limit) continue; /* Free entry */
237 if ((ldt_flags_copy[i] & LDT_FLAGS_TYPE) == SEGMENT_CODE)
239 flags[0] = (ldt_flags_copy[i] & LDT_FLAGS_EXECONLY) ? '-' : 'r';
246 flags[1] = (ldt_flags_copy[i] & LDT_FLAGS_READONLY) ? '-' : 'w';
249 printf("%04x: sel=%04x base=%08lx limit=%08lx %d-bit %c%c%c\n",
250 i, ENTRY_TO_SELECTOR(i),
251 ldt_copy[i].base, ldt_copy[i].limit,
252 ldt_flags_copy[i] & LDT_FLAGS_32BIT ? 32 : 16,
253 flags[0], flags[1], flags[2] );