2 * Selector manipulation functions
4 * Copyright 1995 Alexandre Julliard
10 #include "selectors.h"
11 #include "stackframe.h"
16 #define FIRST_LDT_ENTRY_TO_ALLOC 6
19 /***********************************************************************
20 * AllocSelectorArray (KERNEL.206)
22 WORD
AllocSelectorArray( WORD count
)
27 for (i
= FIRST_LDT_ENTRY_TO_ALLOC
; i
< LDT_SIZE
; i
++)
29 if (!IS_LDT_ENTRY_FREE(i
)) size
= 0;
30 else if (++size
>= count
) break;
32 if (i
== LDT_SIZE
) return 0;
33 /* Mark selector as allocated */
34 while (size
--) ldt_flags_copy
[i
--] |= LDT_FLAGS_ALLOCATED
;
35 return ENTRY_TO_SELECTOR( i
+ 1 );
39 /***********************************************************************
40 * AllocSelector (KERNEL.175)
42 WORD
AllocSelector( WORD sel
)
44 WORD newsel
, count
, i
;
46 count
= sel
? ((GET_SEL_LIMIT(sel
) >> 16) + 1) : 1;
47 newsel
= AllocSelectorArray( count
);
48 dprintf_selector( stddeb
, "AllocSelector(%04x): returning %04x\n",
50 if (!newsel
) return 0;
51 if (!sel
) return newsel
; /* nothing to copy */
52 for (i
= 0; i
< count
; i
++)
55 LDT_GetEntry( SELECTOR_TO_ENTRY(sel
) + i
, &entry
);
56 LDT_SetEntry( SELECTOR_TO_ENTRY(newsel
) + i
, &entry
);
62 /***********************************************************************
63 * FreeSelector (KERNEL.176)
65 WORD
FreeSelector( WORD sel
)
71 dprintf_selector( stddeb
, "FreeSelector(%04x)\n", sel
);
72 if (IS_SELECTOR_FREE(sel
)) return sel
; /* error */
73 count
= (GET_SEL_LIMIT(sel
) >> 16) + 1;
74 memset( &entry
, 0, sizeof(entry
) ); /* clear the LDT entries */
75 /* FIXME: is it correct to free the whole array? */
76 for (i
= SELECTOR_TO_ENTRY(sel
); count
; i
++, count
--)
78 LDT_SetEntry( i
, &entry
);
79 ldt_flags_copy
[i
] &= ~LDT_FLAGS_ALLOCATED
;
82 /* Clear the saved 16-bit selector */
84 frame
= CURRENT_STACK16
;
87 if (frame
->ds
== sel
) frame
->ds
= 0;
88 if (frame
->es
== sel
) frame
->es
= 0;
89 frame
= PTR_SEG_OFF_TO_LIN(frame
->saved_ss
, frame
->saved_sp
);
96 /***********************************************************************
99 * Set the LDT entries for an array of selectors.
101 static void SELECTOR_SetEntries( WORD sel
, const void *base
, DWORD size
,
102 enum seg_type type
, BOOL is32bit
,
108 /* The limit for the first selector is the whole */
109 /* block. The next selectors get a 64k limit. */
110 entry
.base
= (unsigned long)base
;
112 entry
.seg_32bit
= is32bit
;
113 entry
.read_only
= readonly
;
114 entry
.limit_in_pages
= (size
> 0x100000);
115 if (entry
.limit_in_pages
) entry
.limit
= ((size
+ 0xfff) >> 12) - 1;
116 else entry
.limit
= size
- 1;
117 /* Make sure base and limit are not 0 together if the size is not 0 */
118 if (!base
&& !entry
.limit
&& size
) entry
.limit
= 1;
119 count
= (size
+ 0xffff) / 0x10000;
120 for (i
= 0; i
< count
; i
++)
122 LDT_SetEntry( SELECTOR_TO_ENTRY(sel
) + i
, &entry
);
123 entry
.base
+= 0x10000;
125 entry
.limit
= (size
> 0x10000) ? 0xffff : size
-1;
126 entry
.limit_in_pages
= 0;
131 /***********************************************************************
132 * SELECTOR_AllocBlock
134 * Allocate selectors for a block of linear memory.
136 WORD
SELECTOR_AllocBlock( const void *base
, DWORD size
, enum seg_type type
,
137 BOOL is32bit
, BOOL readonly
)
142 count
= (size
+ 0xffff) / 0x10000;
143 sel
= AllocSelectorArray( count
);
144 if (sel
) SELECTOR_SetEntries( sel
, base
, size
, type
, is32bit
, readonly
);
149 /***********************************************************************
150 * SELECTOR_ReallocBlock
152 * Change the size of a block of selectors.
154 WORD
SELECTOR_ReallocBlock( WORD sel
, const void *base
, DWORD size
,
155 enum seg_type type
, BOOL is32bit
, BOOL readonly
)
157 WORD i
, oldcount
, newcount
;
161 oldcount
= (GET_SEL_LIMIT(sel
) >> 16) + 1;
162 newcount
= (size
+ 0xffff) >> 16;
164 if (oldcount
< newcount
) /* We need to add selectors */
166 /* Check if the next selectors are free */
167 if (SELECTOR_TO_ENTRY(sel
) + newcount
> LDT_SIZE
) i
= oldcount
;
169 for (i
= oldcount
; i
< newcount
; i
++)
170 if (!IS_LDT_ENTRY_FREE(SELECTOR_TO_ENTRY(sel
)+i
)) break;
172 if (i
< newcount
) /* they are not free */
175 sel
= AllocSelectorArray( newcount
);
177 else /* mark the selectors as allocated */
179 for (i
= oldcount
; i
< newcount
; i
++)
180 ldt_flags_copy
[SELECTOR_TO_ENTRY(sel
)+i
] |=LDT_FLAGS_ALLOCATED
;
183 else if (oldcount
> newcount
) /* We need to remove selectors */
185 memset( &entry
, 0, sizeof(entry
) ); /* clear the LDT entries */
186 for (i
= oldcount
; i
< newcount
; i
++)
188 LDT_SetEntry( SELECTOR_TO_ENTRY(sel
) + i
, &entry
);
189 ldt_flags_copy
[SELECTOR_TO_ENTRY(sel
) + i
] &= ~LDT_FLAGS_ALLOCATED
;
192 if (sel
) SELECTOR_SetEntries( sel
, base
, size
, type
, is32bit
, readonly
);
197 /***********************************************************************
198 * PrestoChangoSelector (KERNEL.177)
200 WORD
PrestoChangoSelector( WORD selSrc
, WORD selDst
)
203 LDT_GetEntry( SELECTOR_TO_ENTRY( selSrc
), &entry
);
204 entry
.type
^= SEGMENT_CODE
; /* toggle the executable bit */
205 LDT_SetEntry( SELECTOR_TO_ENTRY( selDst
), &entry
);
210 /***********************************************************************
211 * AllocCStoDSAlias (KERNEL.170)
213 WORD
AllocCStoDSAlias( WORD sel
)
218 newsel
= AllocSelectorArray( 1 );
219 dprintf_selector( stddeb
, "AllocCStoDSAlias(%04x): returning %04x\n",
221 if (!newsel
) return 0;
222 LDT_GetEntry( SELECTOR_TO_ENTRY(sel
), &entry
);
223 entry
.type
= SEGMENT_DATA
;
224 LDT_SetEntry( SELECTOR_TO_ENTRY(newsel
), &entry
);
229 /***********************************************************************
230 * AllocDStoCSAlias (KERNEL.171)
232 WORD
AllocDStoCSAlias( WORD sel
)
237 newsel
= AllocSelectorArray( 1 );
238 dprintf_selector( stddeb
, "AllocDStoCSAlias(%04x): returning %04x\n",
240 if (!newsel
) return 0;
241 LDT_GetEntry( SELECTOR_TO_ENTRY(sel
), &entry
);
242 entry
.type
= SEGMENT_CODE
;
243 LDT_SetEntry( SELECTOR_TO_ENTRY(newsel
), &entry
);
248 /***********************************************************************
249 * LongPtrAdd (KERNEL.180)
251 void LongPtrAdd( DWORD ptr
, DWORD add
)
254 LDT_GetEntry( SELECTOR_TO_ENTRY(SELECTOROF(ptr
)), &entry
);
256 LDT_SetEntry( SELECTOR_TO_ENTRY(SELECTOROF(ptr
)), &entry
);
260 /***********************************************************************
261 * GetSelectorBase (KERNEL.186)
263 DWORD
GetSelectorBase( WORD sel
)
265 return GET_SEL_BASE(sel
);
269 /***********************************************************************
270 * SetSelectorBase (KERNEL.187)
272 WORD
SetSelectorBase( WORD sel
, DWORD base
)
275 LDT_GetEntry( SELECTOR_TO_ENTRY(sel
), &entry
);
277 LDT_SetEntry( SELECTOR_TO_ENTRY(sel
), &entry
);
282 /***********************************************************************
283 * GetSelectorLimit (KERNEL.188)
285 DWORD
GetSelectorLimit( WORD sel
)
287 return GET_SEL_LIMIT(sel
);
291 /***********************************************************************
292 * SetSelectorLimit (KERNEL.189)
294 WORD
SetSelectorLimit( WORD sel
, DWORD limit
)
297 LDT_GetEntry( SELECTOR_TO_ENTRY(sel
), &entry
);
299 LDT_SetEntry( SELECTOR_TO_ENTRY(sel
), &entry
);
304 /***********************************************************************
305 * SelectorAccessRights (KERNEL.196)
307 WORD
SelectorAccessRights( WORD sel
, WORD op
, WORD val
)
310 LDT_GetEntry( SELECTOR_TO_ENTRY(sel
), &entry
);
311 if (op
== 0) /* get */
313 return 0x01 | /* accessed */
314 0x10 | /* not system */
317 ((entry
.read_only
== 0) << 1) |
319 (entry
.seg_32bit
<< 14) |
320 (entry
.limit_in_pages
<< 15);
324 entry
.read_only
= ((val
& 2) == 0);
325 entry
.type
= (val
>> 2) & 3;
326 entry
.seg_32bit
= val
& 0x4000;
327 entry
.limit_in_pages
= val
& 0x8000;
328 LDT_SetEntry( SELECTOR_TO_ENTRY(sel
), &entry
);
334 /***********************************************************************
335 * IsBadCodePtr (KERNEL.336)
337 BOOL
IsBadCodePtr( SEGPTR lpfn
)
342 sel
= SELECTOROF(lpfn
);
343 if (!sel
) return TRUE
;
344 if (IS_SELECTOR_FREE(sel
)) return TRUE
;
345 LDT_GetEntry( SELECTOR_TO_ENTRY(sel
), &entry
);
346 if (entry
.type
!= SEGMENT_CODE
) return TRUE
;
347 if (OFFSETOF(lpfn
) > entry
.limit
) return TRUE
;
352 /***********************************************************************
353 * IsBadStringPtr (KERNEL.337)
355 BOOL
IsBadStringPtr( SEGPTR ptr
, WORD size
)
360 sel
= SELECTOROF(ptr
);
361 if (!sel
) return TRUE
;
362 if (IS_SELECTOR_FREE(sel
)) return TRUE
;
363 LDT_GetEntry( SELECTOR_TO_ENTRY(sel
), &entry
);
364 if ((entry
.type
== SEGMENT_CODE
) && entry
.read_only
) return TRUE
;
365 if (strlen(PTR_SEG_TO_LIN(ptr
)) < size
) size
= strlen(PTR_SEG_TO_LIN(ptr
));
366 if (OFFSETOF(ptr
) + size
- 1 > entry
.limit
) return TRUE
;
371 /***********************************************************************
372 * IsBadHugeReadPtr (KERNEL.346)
374 BOOL
IsBadHugeReadPtr( SEGPTR ptr
, DWORD size
)
379 sel
= SELECTOROF(ptr
);
380 if (!sel
) return TRUE
;
381 if (IS_SELECTOR_FREE(sel
)) return TRUE
;
382 LDT_GetEntry( SELECTOR_TO_ENTRY(sel
), &entry
);
383 if ((entry
.type
== SEGMENT_CODE
) && entry
.read_only
) return TRUE
;
384 if (OFFSETOF(ptr
) + size
- 1 > entry
.limit
) return TRUE
;
389 /***********************************************************************
390 * IsBadHugeWritePtr (KERNEL.347)
392 BOOL
IsBadHugeWritePtr( SEGPTR ptr
, DWORD size
)
397 sel
= SELECTOROF(ptr
);
398 if (!sel
) return TRUE
;
399 if (IS_SELECTOR_FREE(sel
)) return TRUE
;
400 LDT_GetEntry( SELECTOR_TO_ENTRY(sel
), &entry
);
401 if ((entry
.type
== SEGMENT_CODE
) || entry
.read_only
) return TRUE
;
402 if (OFFSETOF(ptr
) + size
- 1 > entry
.limit
) return TRUE
;
406 /***********************************************************************
407 * IsBadReadPtr (KERNEL.334)
409 BOOL
IsBadReadPtr( SEGPTR ptr
, WORD size
)
411 return IsBadHugeReadPtr( ptr
, size
);
415 /***********************************************************************
416 * IsBadWritePtr (KERNEL.335)
418 BOOL
IsBadWritePtr( SEGPTR ptr
, WORD size
)
420 return IsBadHugeWritePtr( ptr
, size
);
424 /***********************************************************************
425 * MemoryRead (TOOLHELP.78)
427 DWORD
MemoryRead( WORD sel
, DWORD offset
, void *buffer
, DWORD count
)
429 if (IS_SELECTOR_FREE(sel
)) return 0;
430 if (offset
> GET_SEL_LIMIT(sel
)) return 0;
431 if (offset
+ count
> GET_SEL_LIMIT(sel
) + 1)
432 count
= GET_SEL_LIMIT(sel
) + 1 - offset
;
433 memcpy( buffer
, ((char *)GET_SEL_BASE(sel
)) + offset
, count
);
438 /***********************************************************************
439 * MemoryWrite (TOOLHELP.79)
441 DWORD
MemoryWrite( WORD sel
, DWORD offset
, void *buffer
, DWORD count
)
443 if (IS_SELECTOR_FREE(sel
)) return 0;
444 if (offset
> GET_SEL_LIMIT(sel
)) return 0;
445 if (offset
+ count
> GET_SEL_LIMIT(sel
) + 1)
446 count
= GET_SEL_LIMIT(sel
) + 1 - offset
;
447 memcpy( ((char *)GET_SEL_BASE(sel
)) + offset
, buffer
, count
);
452 SEGPTR
MAKE_SEGPTR(void * ptr
)
460 if (!((unsigned)ptr
& 0xffff0000)) {
461 fprintf(stderr
, "Invalid pointer %p has been passed to MAKE_SEGPTR. This was\n", ptr
);
462 fprintf(stderr
, "probably caused by an unnecessary call to PTR_SEG_TO_LIN.\n");
463 fprintf(stderr
, "Forcing call to debugger\n");
466 result
= (SEGPTR
) (IF1632_Stack32_base
) +
467 ((DWORD
)(ptr
) - (DWORD
) PTR_SEG_TO_LIN(IF1632_Stack32_base
));
468 if (PTR_SEG_TO_LIN(result
) == ptr
)
471 for (entry
= 0; entry
< LDT_SIZE
; entry
++) {
472 if (ldt_copy
[entry
].base
&&
473 (ldt_copy
[entry
].limit
< 0x10000) &&
474 ((unsigned) ptr
>= ldt_copy
[entry
].base
) &&
475 ((unsigned) ptr
< (ldt_copy
[entry
].base
+ ldt_copy
[entry
].limit
))) {
476 return ((ENTRY_TO_SELECTOR(entry
) << 16) |
477 ((unsigned) ptr
- ldt_copy
[entry
].base
));
480 entry
= SELECTOR_AllocBlock((void *)((unsigned)ptr
& 0xffff0000), 0x10000, SEGMENT_DATA
, 0, 0);
481 return ((entry
<< 16) | ((unsigned) ptr
& 0xffff));