Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Selector manipulation functions |
| 3 | * |
| 4 | * Copyright 1995 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2.1 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Francois Gouget | e5ddd26 | 2001-10-14 16:18:52 +0000 | [diff] [blame] | 21 | #include "config.h" |
Alexandre Julliard | 894b188 | 2002-04-25 21:40:56 +0000 | [diff] [blame] | 22 | #include "wine/port.h" |
Francois Gouget | e5ddd26 | 2001-10-14 16:18:52 +0000 | [diff] [blame] | 23 | |
Alexandre Julliard | fa68b75 | 1995-04-03 16:55:37 +0000 | [diff] [blame] | 24 | #include <string.h> |
Dimitrie O. Paun | 0b7a7bb | 2000-11-25 01:31:17 +0000 | [diff] [blame] | 25 | |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 26 | #include "winerror.h" |
Marcus Meissner | 04c3e1d | 1999-02-19 10:37:02 +0000 | [diff] [blame] | 27 | #include "wine/winbase16.h" |
Alexandre Julliard | 37e9503 | 2001-07-19 00:39:09 +0000 | [diff] [blame] | 28 | #include "wine/server.h" |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 29 | #include "wine/debug.h" |
Alexandre Julliard | a7ce2ab | 2003-11-18 00:16:25 +0000 | [diff] [blame] | 30 | #include "kernel_private.h" |
Patrik Stridvall | 1ed4ecf | 1999-06-26 14:58:24 +0000 | [diff] [blame] | 31 | #include "toolhelp.h" |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 32 | |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 33 | WINE_DEFAULT_DEBUG_CHANNEL(selector); |
Patrik Stridvall | b4b9fae | 1999-04-19 14:56:29 +0000 | [diff] [blame] | 34 | |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 35 | #define LDT_SIZE 8192 |
| 36 | |
| 37 | /* get the number of selectors needed to cover up to the selector limit */ |
| 38 | inline static WORD get_sel_count( WORD sel ) |
| 39 | { |
| 40 | return (wine_ldt_copy.limit[sel >> __AHSHIFT] >> 16) + 1; |
| 41 | } |
| 42 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 43 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 44 | /*********************************************************************** |
| 45 | * AllocSelectorArray (KERNEL.206) |
| 46 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 47 | WORD WINAPI AllocSelectorArray16( WORD count ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 48 | { |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 49 | WORD i, sel = wine_ldt_alloc_entries( count ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 50 | |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 51 | if (sel) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 52 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 53 | LDT_ENTRY entry; |
| 54 | wine_ldt_set_base( &entry, 0 ); |
| 55 | wine_ldt_set_limit( &entry, 1 ); /* avoid 0 base and limit */ |
| 56 | wine_ldt_set_flags( &entry, WINE_LDT_FLAGS_DATA ); |
| 57 | for (i = 0; i < count; i++) wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 58 | } |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 59 | return sel; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
| 63 | /*********************************************************************** |
| 64 | * AllocSelector (KERNEL.175) |
| 65 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 66 | WORD WINAPI AllocSelector16( WORD sel ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 67 | { |
| 68 | WORD newsel, count, i; |
| 69 | |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 70 | count = sel ? get_sel_count(sel) : 1; |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 71 | newsel = wine_ldt_alloc_entries( count ); |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 72 | TRACE("(%04x): returning %04x\n", sel, newsel ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 73 | if (!newsel) return 0; |
| 74 | if (!sel) return newsel; /* nothing to copy */ |
| 75 | for (i = 0; i < count; i++) |
| 76 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 77 | LDT_ENTRY entry; |
| 78 | wine_ldt_get_entry( sel + (i << __AHSHIFT), &entry ); |
| 79 | wine_ldt_set_entry( newsel + (i << __AHSHIFT), &entry ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 80 | } |
| 81 | return newsel; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /*********************************************************************** |
| 86 | * FreeSelector (KERNEL.176) |
| 87 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 88 | WORD WINAPI FreeSelector16( WORD sel ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 89 | { |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 90 | LDT_ENTRY entry; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 91 | |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 92 | wine_ldt_get_entry( sel, &entry ); |
| 93 | if (wine_ldt_is_empty( &entry )) return sel; /* error */ |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 94 | #ifdef __i386__ |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 95 | /* Check if we are freeing current %fs selector */ |
Alexandre Julliard | 4ced7ef | 2002-08-14 21:10:50 +0000 | [diff] [blame] | 96 | if (!((wine_get_fs() ^ sel) & ~3)) |
Alexandre Julliard | 5900867 | 2002-05-16 20:32:16 +0000 | [diff] [blame] | 97 | WARN("Freeing %%fs selector (%04x), not good.\n", wine_get_fs() ); |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 98 | #endif /* __i386__ */ |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 99 | wine_ldt_free_entries( sel, 1 ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /*********************************************************************** |
| 105 | * SELECTOR_SetEntries |
| 106 | * |
| 107 | * Set the LDT entries for an array of selectors. |
| 108 | */ |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 109 | static void SELECTOR_SetEntries( WORD sel, const void *base, DWORD size, unsigned char flags ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 110 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 111 | LDT_ENTRY entry; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 112 | WORD i, count; |
| 113 | |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 114 | wine_ldt_set_base( &entry, base ); |
Alexandre Julliard | 4ced7ef | 2002-08-14 21:10:50 +0000 | [diff] [blame] | 115 | wine_ldt_set_limit( &entry, size - 1 ); |
Andreas Mohr | 757e7cb | 2002-05-08 00:20:40 +0000 | [diff] [blame] | 116 | wine_ldt_set_flags( &entry, flags ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 117 | count = (size + 0xffff) / 0x10000; |
| 118 | for (i = 0; i < count; i++) |
| 119 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 120 | wine_ldt_set_entry( sel + (i << __AHSHIFT), &entry ); |
Gregg Mattinson | 7c4cb51 | 2002-07-03 21:10:43 +0000 | [diff] [blame] | 121 | wine_ldt_set_base( &entry, (char*)wine_ldt_get_base(&entry) + 0x10000); |
Andreas Mohr | 757e7cb | 2002-05-08 00:20:40 +0000 | [diff] [blame] | 122 | /* yep, Windows sets limit like that, not 64K sel units */ |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 123 | wine_ldt_set_limit( &entry, wine_ldt_get_limit(&entry) - 0x10000 ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /*********************************************************************** |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 129 | * SELECTOR_AllocBlock |
| 130 | * |
| 131 | * Allocate selectors for a block of linear memory. |
| 132 | */ |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 133 | WORD SELECTOR_AllocBlock( const void *base, DWORD size, unsigned char flags ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 134 | { |
| 135 | WORD sel, count; |
| 136 | |
| 137 | if (!size) return 0; |
| 138 | count = (size + 0xffff) / 0x10000; |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 139 | sel = wine_ldt_alloc_entries( count ); |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 140 | if (sel) SELECTOR_SetEntries( sel, base, size, flags ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 141 | return sel; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /*********************************************************************** |
Alexandre Julliard | 8cc3a5e | 1996-08-11 15:49:51 +0000 | [diff] [blame] | 146 | * SELECTOR_FreeBlock |
| 147 | * |
| 148 | * Free a block of selectors. |
| 149 | */ |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 150 | void SELECTOR_FreeBlock( WORD sel ) |
Alexandre Julliard | 8cc3a5e | 1996-08-11 15:49:51 +0000 | [diff] [blame] | 151 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 152 | WORD i, count = get_sel_count( sel ); |
Alexandre Julliard | 8cc3a5e | 1996-08-11 15:49:51 +0000 | [diff] [blame] | 153 | |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 154 | TRACE("(%04x,%d)\n", sel, count ); |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 155 | for (i = 0; i < count; i++) FreeSelector16( sel + (i << __AHSHIFT) ); |
Alexandre Julliard | 8cc3a5e | 1996-08-11 15:49:51 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
| 159 | /*********************************************************************** |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 160 | * SELECTOR_ReallocBlock |
| 161 | * |
| 162 | * Change the size of a block of selectors. |
| 163 | */ |
Alexandre Julliard | 284c9b9 | 1999-04-11 15:07:13 +0000 | [diff] [blame] | 164 | WORD SELECTOR_ReallocBlock( WORD sel, const void *base, DWORD size ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 165 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 166 | LDT_ENTRY entry; |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 167 | int oldcount, newcount; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 168 | |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 169 | if (!size) size = 1; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 170 | wine_ldt_get_entry( sel, &entry ); |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 171 | oldcount = (wine_ldt_get_limit(&entry) >> 16) + 1; |
| 172 | newcount = (size + 0xffff) >> 16; |
Alexandre Julliard | e2991ea | 1995-07-29 13:09:43 +0000 | [diff] [blame] | 173 | |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 174 | sel = wine_ldt_realloc_entries( sel, oldcount, newcount ); |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 175 | if (sel) SELECTOR_SetEntries( sel, base, size, wine_ldt_get_flags(&entry) ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 176 | return sel; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /*********************************************************************** |
| 181 | * PrestoChangoSelector (KERNEL.177) |
| 182 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 183 | WORD WINAPI PrestoChangoSelector16( WORD selSrc, WORD selDst ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 184 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 185 | LDT_ENTRY entry; |
| 186 | wine_ldt_get_entry( selSrc, &entry ); |
| 187 | /* toggle the executable bit */ |
| 188 | entry.HighWord.Bits.Type ^= (WINE_LDT_FLAGS_CODE ^ WINE_LDT_FLAGS_DATA); |
| 189 | wine_ldt_set_entry( selDst, &entry ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 190 | return selDst; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 195 | * AllocCStoDSAlias (KERNEL.170) |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 196 | * AllocAlias (KERNEL.172) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 197 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 198 | WORD WINAPI AllocCStoDSAlias16( WORD sel ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 199 | { |
| 200 | WORD newsel; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 201 | LDT_ENTRY entry; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 202 | |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 203 | newsel = wine_ldt_alloc_entries( 1 ); |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 204 | TRACE("(%04x): returning %04x\n", |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 205 | sel, newsel ); |
| 206 | if (!newsel) return 0; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 207 | wine_ldt_get_entry( sel, &entry ); |
| 208 | entry.HighWord.Bits.Type = WINE_LDT_FLAGS_DATA; |
| 209 | wine_ldt_set_entry( newsel, &entry ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 210 | return newsel; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | /*********************************************************************** |
| 215 | * AllocDStoCSAlias (KERNEL.171) |
| 216 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 217 | WORD WINAPI AllocDStoCSAlias16( WORD sel ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 218 | { |
| 219 | WORD newsel; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 220 | LDT_ENTRY entry; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 221 | |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 222 | newsel = wine_ldt_alloc_entries( 1 ); |
Alexandre Julliard | 1565709 | 1999-05-23 10:25:25 +0000 | [diff] [blame] | 223 | TRACE("(%04x): returning %04x\n", |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 224 | sel, newsel ); |
| 225 | if (!newsel) return 0; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 226 | wine_ldt_get_entry( sel, &entry ); |
| 227 | entry.HighWord.Bits.Type = WINE_LDT_FLAGS_CODE; |
| 228 | wine_ldt_set_entry( newsel, &entry ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 229 | return newsel; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /*********************************************************************** |
| 234 | * LongPtrAdd (KERNEL.180) |
| 235 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 236 | void WINAPI LongPtrAdd16( DWORD ptr, DWORD add ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 237 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 238 | LDT_ENTRY entry; |
| 239 | wine_ldt_get_entry( SELECTOROF(ptr), &entry ); |
| 240 | wine_ldt_set_base( &entry, (char *)wine_ldt_get_base(&entry) + add ); |
| 241 | wine_ldt_set_entry( SELECTOROF(ptr), &entry ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | |
| 245 | /*********************************************************************** |
Jukka Heinonen | 56dff0d | 2002-11-20 19:40:12 +0000 | [diff] [blame] | 246 | * GetSelectorBase (KERNEL.186) |
Patrik Stridvall | b5bb32c | 2002-09-24 18:29:39 +0000 | [diff] [blame] | 247 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 248 | DWORD WINAPI GetSelectorBase( WORD sel ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 249 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 250 | void *base = wine_ldt_copy.base[sel >> __AHSHIFT]; |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 251 | |
Alexandre Julliard | ac9c9b0 | 1996-07-28 18:50:11 +0000 | [diff] [blame] | 252 | /* if base points into DOSMEM, assume we have to |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 253 | * return pointer into physical lower 1MB */ |
| 254 | |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 255 | return DOSMEM_MapLinearToDos( base ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | |
| 259 | /*********************************************************************** |
Jukka Heinonen | 56dff0d | 2002-11-20 19:40:12 +0000 | [diff] [blame] | 260 | * SetSelectorBase (KERNEL.187) |
Patrik Stridvall | b5bb32c | 2002-09-24 18:29:39 +0000 | [diff] [blame] | 261 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 262 | WORD WINAPI SetSelectorBase( WORD sel, DWORD base ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 263 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 264 | LDT_ENTRY entry; |
| 265 | wine_ldt_get_entry( sel, &entry ); |
| 266 | wine_ldt_set_base( &entry, DOSMEM_MapDosToLinear(base) ); |
| 267 | wine_ldt_set_entry( sel, &entry ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 268 | return sel; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | /*********************************************************************** |
| 273 | * GetSelectorLimit (KERNEL.188) |
| 274 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 275 | DWORD WINAPI GetSelectorLimit16( WORD sel ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 276 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 277 | return wine_ldt_copy.limit[sel >> __AHSHIFT]; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | |
| 281 | /*********************************************************************** |
| 282 | * SetSelectorLimit (KERNEL.189) |
| 283 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 284 | WORD WINAPI SetSelectorLimit16( WORD sel, DWORD limit ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 285 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 286 | LDT_ENTRY entry; |
| 287 | wine_ldt_get_entry( sel, &entry ); |
| 288 | wine_ldt_set_limit( &entry, limit ); |
| 289 | wine_ldt_set_entry( sel, &entry ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 290 | return sel; |
| 291 | } |
| 292 | |
| 293 | |
| 294 | /*********************************************************************** |
| 295 | * SelectorAccessRights (KERNEL.196) |
| 296 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 297 | WORD WINAPI SelectorAccessRights16( WORD sel, WORD op, WORD val ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 298 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 299 | LDT_ENTRY entry; |
| 300 | wine_ldt_get_entry( sel, &entry ); |
| 301 | |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 302 | if (op == 0) /* get */ |
| 303 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 304 | return entry.HighWord.Bytes.Flags1 | ((entry.HighWord.Bytes.Flags2 << 8) & 0xf0); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 305 | } |
| 306 | else /* set */ |
| 307 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 308 | entry.HighWord.Bytes.Flags1 = LOBYTE(val) | 0xf0; |
| 309 | entry.HighWord.Bytes.Flags2 = (entry.HighWord.Bytes.Flags2 & 0x0f) | (HIBYTE(val) & 0xf0); |
| 310 | wine_ldt_set_entry( sel, &entry ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 311 | return 0; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | |
| 316 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 317 | * IsBadCodePtr (KERNEL.336) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 318 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 319 | BOOL16 WINAPI IsBadCodePtr16( SEGPTR lpfn ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 320 | { |
| 321 | WORD sel; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 322 | LDT_ENTRY entry; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 323 | |
| 324 | sel = SELECTOROF(lpfn); |
Alexandre Julliard | bd34d4f | 1995-06-20 19:08:12 +0000 | [diff] [blame] | 325 | if (!sel) return TRUE; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 326 | wine_ldt_get_entry( sel, &entry ); |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 327 | if (wine_ldt_is_empty( &entry )) return TRUE; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 328 | /* check for code segment, ignoring conforming, read-only and accessed bits */ |
| 329 | if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_CODE) & 0x18) return TRUE; |
| 330 | if (OFFSETOF(lpfn) > wine_ldt_get_limit(&entry)) return TRUE; |
Alexandre Julliard | bd34d4f | 1995-06-20 19:08:12 +0000 | [diff] [blame] | 331 | return FALSE; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | |
| 335 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 336 | * IsBadStringPtr (KERNEL.337) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 337 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 338 | BOOL16 WINAPI IsBadStringPtr16( SEGPTR ptr, UINT16 size ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 339 | { |
| 340 | WORD sel; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 341 | LDT_ENTRY entry; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 342 | |
| 343 | sel = SELECTOROF(ptr); |
Alexandre Julliard | bd34d4f | 1995-06-20 19:08:12 +0000 | [diff] [blame] | 344 | if (!sel) return TRUE; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 345 | wine_ldt_get_entry( sel, &entry ); |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 346 | if (wine_ldt_is_empty( &entry )) return TRUE; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 347 | /* check for data or readable code segment */ |
| 348 | if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */ |
| 349 | if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */ |
Alexandre Julliard | 982a223 | 2000-12-13 20:20:09 +0000 | [diff] [blame] | 350 | if (strlen(MapSL(ptr)) < size) size = strlen(MapSL(ptr)) + 1; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 351 | if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit(&entry))) return TRUE; |
Alexandre Julliard | bd34d4f | 1995-06-20 19:08:12 +0000 | [diff] [blame] | 352 | return FALSE; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | |
| 356 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 357 | * IsBadHugeReadPtr (KERNEL.346) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 358 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 359 | BOOL16 WINAPI IsBadHugeReadPtr16( SEGPTR ptr, DWORD size ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 360 | { |
| 361 | WORD sel; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 362 | LDT_ENTRY entry; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 363 | |
| 364 | sel = SELECTOROF(ptr); |
Alexandre Julliard | bd34d4f | 1995-06-20 19:08:12 +0000 | [diff] [blame] | 365 | if (!sel) return TRUE; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 366 | wine_ldt_get_entry( sel, &entry ); |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 367 | if (wine_ldt_is_empty( &entry )) return TRUE; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 368 | /* check for data or readable code segment */ |
| 369 | if (!(entry.HighWord.Bits.Type & 0x10)) return TRUE; /* system descriptor */ |
| 370 | if ((entry.HighWord.Bits.Type & 0x0a) == 0x08) return TRUE; /* non-readable code segment */ |
| 371 | if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE; |
Alexandre Julliard | bd34d4f | 1995-06-20 19:08:12 +0000 | [diff] [blame] | 372 | return FALSE; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | |
| 376 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 377 | * IsBadHugeWritePtr (KERNEL.347) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 378 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 379 | BOOL16 WINAPI IsBadHugeWritePtr16( SEGPTR ptr, DWORD size ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 380 | { |
| 381 | WORD sel; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 382 | LDT_ENTRY entry; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 383 | |
| 384 | sel = SELECTOROF(ptr); |
Alexandre Julliard | bd34d4f | 1995-06-20 19:08:12 +0000 | [diff] [blame] | 385 | if (!sel) return TRUE; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 386 | wine_ldt_get_entry( sel, &entry ); |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 387 | if (wine_ldt_is_empty( &entry )) return TRUE; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 388 | /* check for writeable data segment, ignoring expand-down and accessed flags */ |
| 389 | if ((entry.HighWord.Bits.Type ^ WINE_LDT_FLAGS_DATA) & ~5) return TRUE; |
| 390 | if (size && (OFFSETOF(ptr) + size - 1 > wine_ldt_get_limit( &entry ))) return TRUE; |
Alexandre Julliard | bd34d4f | 1995-06-20 19:08:12 +0000 | [diff] [blame] | 391 | return FALSE; |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 395 | * IsBadReadPtr (KERNEL.334) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 396 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 397 | BOOL16 WINAPI IsBadReadPtr16( SEGPTR ptr, UINT16 size ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 398 | { |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 399 | return IsBadHugeReadPtr16( ptr, size ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | |
| 403 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 404 | * IsBadWritePtr (KERNEL.335) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 405 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 406 | BOOL16 WINAPI IsBadWritePtr16( SEGPTR ptr, UINT16 size ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 407 | { |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame] | 408 | return IsBadHugeWritePtr16( ptr, size ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | |
| 412 | /*********************************************************************** |
Patrik Stridvall | 01d5e5b | 2001-07-02 19:59:40 +0000 | [diff] [blame] | 413 | * IsBadFlatReadWritePtr (KERNEL.627) |
Ulrich Weigand | 98b5f77 | 1999-07-27 17:09:14 +0000 | [diff] [blame] | 414 | */ |
| 415 | BOOL16 WINAPI IsBadFlatReadWritePtr16( SEGPTR ptr, DWORD size, BOOL16 bWrite ) |
| 416 | { |
| 417 | return bWrite? IsBadHugeWritePtr16( ptr, size ) |
| 418 | : IsBadHugeReadPtr16( ptr, size ); |
| 419 | } |
| 420 | |
| 421 | |
| 422 | /*********************************************************************** |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 423 | * MemoryRead (TOOLHELP.78) |
| 424 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 425 | DWORD WINAPI MemoryRead16( WORD sel, DWORD offset, void *buffer, DWORD count ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 426 | { |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 427 | LDT_ENTRY entry; |
| 428 | DWORD limit; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 429 | |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 430 | wine_ldt_get_entry( sel, &entry ); |
| 431 | if (wine_ldt_is_empty( &entry )) return 0; |
| 432 | limit = wine_ldt_get_limit( &entry ); |
| 433 | if (offset > limit) return 0; |
| 434 | if (offset + count > limit + 1) count = limit + 1 - offset; |
| 435 | memcpy( buffer, (char *)wine_ldt_get_base(&entry) + offset, count ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 436 | return count; |
| 437 | } |
| 438 | |
| 439 | |
| 440 | /*********************************************************************** |
| 441 | * MemoryWrite (TOOLHELP.79) |
| 442 | */ |
Alexandre Julliard | a396029 | 1999-02-26 11:11:13 +0000 | [diff] [blame] | 443 | DWORD WINAPI MemoryWrite16( WORD sel, DWORD offset, void *buffer, DWORD count ) |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 444 | { |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 445 | LDT_ENTRY entry; |
| 446 | DWORD limit; |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 447 | |
Alexandre Julliard | ce13153 | 2003-02-26 20:34:45 +0000 | [diff] [blame] | 448 | wine_ldt_get_entry( sel, &entry ); |
| 449 | if (wine_ldt_is_empty( &entry )) return 0; |
| 450 | limit = wine_ldt_get_limit( &entry ); |
| 451 | if (offset > limit) return 0; |
| 452 | if (offset + count > limit) count = limit + 1 - offset; |
| 453 | memcpy( (char *)wine_ldt_get_base(&entry) + offset, buffer, count ); |
Alexandre Julliard | e2abbb1 | 1995-03-19 17:39:39 +0000 | [diff] [blame] | 454 | return count; |
| 455 | } |
Alexandre Julliard | df2673b | 1997-03-29 17:20:20 +0000 | [diff] [blame] | 456 | |
| 457 | /************************************* Win95 pointer mapping functions * |
| 458 | * |
Alexandre Julliard | df2673b | 1997-03-29 17:20:20 +0000 | [diff] [blame] | 459 | */ |
| 460 | |
Alexandre Julliard | b0f5861 | 2001-12-19 19:16:27 +0000 | [diff] [blame] | 461 | struct mapls_entry |
| 462 | { |
| 463 | struct mapls_entry *next; |
| 464 | void *addr; /* linear address */ |
| 465 | int count; /* ref count */ |
| 466 | WORD sel; /* selector */ |
| 467 | }; |
| 468 | |
| 469 | static struct mapls_entry *first_entry; |
| 470 | |
| 471 | |
| 472 | /*********************************************************************** |
| 473 | * MapLS (KERNEL32.@) |
| 474 | * MapLS (KERNEL.358) |
| 475 | * |
| 476 | * Maps linear pointer to segmented. |
| 477 | */ |
| 478 | SEGPTR WINAPI MapLS( LPCVOID ptr ) |
| 479 | { |
| 480 | struct mapls_entry *entry, *free = NULL; |
Eric Pouech | 62a8609 | 2004-06-14 17:04:34 +0000 | [diff] [blame] | 481 | const void *base; |
Alexandre Julliard | b0f5861 | 2001-12-19 19:16:27 +0000 | [diff] [blame] | 482 | SEGPTR ret = 0; |
| 483 | |
Alexandre Julliard | ba41fe2 | 2005-09-12 21:22:32 +0000 | [diff] [blame] | 484 | if (!HIWORD(ptr)) return (SEGPTR)LOWORD(ptr); |
Alexandre Julliard | b0f5861 | 2001-12-19 19:16:27 +0000 | [diff] [blame] | 485 | |
Eric Pouech | 62a8609 | 2004-06-14 17:04:34 +0000 | [diff] [blame] | 486 | base = (const char *)ptr - ((unsigned int)ptr & 0x7fff); |
Alexandre Julliard | b0f5861 | 2001-12-19 19:16:27 +0000 | [diff] [blame] | 487 | HeapLock( GetProcessHeap() ); |
| 488 | for (entry = first_entry; entry; entry = entry->next) |
| 489 | { |
| 490 | if (entry->addr == base) break; |
| 491 | if (!entry->count) free = entry; |
| 492 | } |
| 493 | |
| 494 | if (!entry) |
| 495 | { |
| 496 | if (!free) /* no free entry found, create a new one */ |
| 497 | { |
| 498 | if (!(free = HeapAlloc( GetProcessHeap(), 0, sizeof(*free) ))) goto done; |
| 499 | if (!(free->sel = SELECTOR_AllocBlock( base, 0x10000, WINE_LDT_FLAGS_DATA ))) |
| 500 | { |
| 501 | HeapFree( GetProcessHeap(), 0, free ); |
| 502 | goto done; |
| 503 | } |
| 504 | free->count = 0; |
| 505 | free->next = first_entry; |
| 506 | first_entry = free; |
| 507 | } |
| 508 | SetSelectorBase( free->sel, (DWORD)base ); |
Eric Pouech | 62a8609 | 2004-06-14 17:04:34 +0000 | [diff] [blame] | 509 | free->addr = (void*)base; |
Alexandre Julliard | b0f5861 | 2001-12-19 19:16:27 +0000 | [diff] [blame] | 510 | entry = free; |
| 511 | } |
| 512 | entry->count++; |
Eric Pouech | 62a8609 | 2004-06-14 17:04:34 +0000 | [diff] [blame] | 513 | ret = MAKESEGPTR( entry->sel, (const char *)ptr - (char *)entry->addr ); |
Alexandre Julliard | b0f5861 | 2001-12-19 19:16:27 +0000 | [diff] [blame] | 514 | done: |
| 515 | HeapUnlock( GetProcessHeap() ); |
| 516 | return ret; |
| 517 | } |
| 518 | |
| 519 | /*********************************************************************** |
| 520 | * UnMapLS (KERNEL32.@) |
| 521 | * UnMapLS (KERNEL.359) |
| 522 | * |
| 523 | * Free mapped selector. |
| 524 | */ |
| 525 | void WINAPI UnMapLS( SEGPTR sptr ) |
| 526 | { |
| 527 | struct mapls_entry *entry; |
| 528 | WORD sel = SELECTOROF(sptr); |
| 529 | |
| 530 | if (sel) |
| 531 | { |
| 532 | HeapLock( GetProcessHeap() ); |
| 533 | for (entry = first_entry; entry; entry = entry->next) if (entry->sel == sel) break; |
| 534 | if (entry && entry->count > 0) entry->count--; |
| 535 | HeapUnlock( GetProcessHeap() ); |
| 536 | } |
| 537 | } |
| 538 | |
Alexandre Julliard | df2673b | 1997-03-29 17:20:20 +0000 | [diff] [blame] | 539 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 540 | * MapSL (KERNEL32.@) |
Patrik Stridvall | 044855c | 2001-07-11 18:56:41 +0000 | [diff] [blame] | 541 | * MapSL (KERNEL.357) |
Alexandre Julliard | df2673b | 1997-03-29 17:20:20 +0000 | [diff] [blame] | 542 | * |
| 543 | * Maps fixed segmented pointer to linear. |
| 544 | */ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 545 | LPVOID WINAPI MapSL( SEGPTR sptr ) |
Alexandre Julliard | df2673b | 1997-03-29 17:20:20 +0000 | [diff] [blame] | 546 | { |
Alexandre Julliard | 914406f | 2000-11-14 01:54:49 +0000 | [diff] [blame] | 547 | return (char *)wine_ldt_copy.base[SELECTOROF(sptr) >> __AHSHIFT] + OFFSETOF(sptr); |
Alexandre Julliard | df2673b | 1997-03-29 17:20:20 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 550 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 551 | * MapSLFix (KERNEL32.@) |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 552 | * |
| 553 | * FIXME: MapSLFix and UnMapSLFixArray should probably prevent |
| 554 | * unexpected linear address change when GlobalCompact() shuffles |
| 555 | * moveable blocks. |
| 556 | */ |
| 557 | |
| 558 | LPVOID WINAPI MapSLFix( SEGPTR sptr ) |
| 559 | { |
Alexandre Julliard | 982a223 | 2000-12-13 20:20:09 +0000 | [diff] [blame] | 560 | return MapSL(sptr); |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 561 | } |
Alexandre Julliard | df2673b | 1997-03-29 17:20:20 +0000 | [diff] [blame] | 562 | |
| 563 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 564 | * UnMapSLFixArray (KERNEL32.@) |
Alexandre Julliard | 643463b | 2006-01-13 14:26:24 +0100 | [diff] [blame] | 565 | * |
| 566 | * Must not change EAX, hence defined as asm function. |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 567 | */ |
Alexandre Julliard | 643463b | 2006-01-13 14:26:24 +0100 | [diff] [blame] | 568 | #ifdef __i386__ |
| 569 | __ASM_GLOBAL_FUNC( UnMapSLFixArray, "ret $8" ); |
Alexandre Julliard | 2c7a248 | 2005-05-07 18:25:51 +0000 | [diff] [blame] | 570 | #endif |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 571 | |
Alexandre Julliard | 643463b | 2006-01-13 14:26:24 +0100 | [diff] [blame] | 572 | |
Alexandre Julliard | c7c217b | 1998-04-13 12:21:30 +0000 | [diff] [blame] | 573 | /*********************************************************************** |
Patrik Stridvall | 3ca9823 | 2001-06-20 23:03:14 +0000 | [diff] [blame] | 574 | * GetThreadSelectorEntry (KERNEL32.@) |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 575 | */ |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 576 | BOOL WINAPI GetThreadSelectorEntry( HANDLE hthread, DWORD sel, LPLDT_ENTRY ldtent) |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 577 | { |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 578 | #ifdef __i386__ |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 579 | BOOL ret; |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 580 | |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 581 | if (!(sel & 4)) /* GDT selector */ |
| 582 | { |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 583 | sel &= ~3; /* ignore RPL */ |
| 584 | if (!sel) /* null selector */ |
| 585 | { |
| 586 | memset( ldtent, 0, sizeof(*ldtent) ); |
| 587 | return TRUE; |
| 588 | } |
| 589 | ldtent->BaseLow = 0; |
| 590 | ldtent->HighWord.Bits.BaseMid = 0; |
| 591 | ldtent->HighWord.Bits.BaseHi = 0; |
| 592 | ldtent->LimitLow = 0xffff; |
| 593 | ldtent->HighWord.Bits.LimitHi = 0xf; |
| 594 | ldtent->HighWord.Bits.Dpl = 3; |
| 595 | ldtent->HighWord.Bits.Sys = 0; |
| 596 | ldtent->HighWord.Bits.Pres = 1; |
| 597 | ldtent->HighWord.Bits.Granularity = 1; |
| 598 | ldtent->HighWord.Bits.Default_Big = 1; |
| 599 | ldtent->HighWord.Bits.Type = 0x12; |
| 600 | /* it has to be one of the system GDT selectors */ |
Alexandre Julliard | 5900867 | 2002-05-16 20:32:16 +0000 | [diff] [blame] | 601 | if (sel == (wine_get_ds() & ~3)) return TRUE; |
| 602 | if (sel == (wine_get_ss() & ~3)) return TRUE; |
| 603 | if (sel == (wine_get_cs() & ~3)) |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 604 | { |
| 605 | ldtent->HighWord.Bits.Type |= 8; /* code segment */ |
| 606 | return TRUE; |
| 607 | } |
| 608 | SetLastError( ERROR_NOACCESS ); |
| 609 | return FALSE; |
| 610 | } |
| 611 | |
Alexandre Julliard | 67a7499 | 2001-02-27 02:09:16 +0000 | [diff] [blame] | 612 | SERVER_START_REQ( get_selector_entry ) |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 613 | { |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 614 | req->handle = hthread; |
| 615 | req->entry = sel >> __AHSHIFT; |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 616 | if ((ret = !wine_server_call_err( req ))) |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 617 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 618 | if (!(reply->flags & WINE_LDT_FLAGS_ALLOCATED)) |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 619 | { |
| 620 | SetLastError( ERROR_MR_MID_NOT_FOUND ); /* sic */ |
| 621 | ret = FALSE; |
| 622 | } |
| 623 | else |
| 624 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 625 | wine_ldt_set_base( ldtent, (void *)reply->base ); |
| 626 | wine_ldt_set_limit( ldtent, reply->limit ); |
| 627 | wine_ldt_set_flags( ldtent, reply->flags ); |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 630 | } |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 631 | SERVER_END_REQ; |
| 632 | return ret; |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 633 | #else |
Ulrich Weigand | afd6a4b | 2000-06-04 01:48:05 +0000 | [diff] [blame] | 634 | SetLastError( ERROR_CALL_NOT_IMPLEMENTED ); |
Alexandre Julliard | 0a7c1f6 | 2000-01-27 02:54:17 +0000 | [diff] [blame] | 635 | return FALSE; |
| 636 | #endif |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | |
Alexandre Julliard | 643463b | 2006-01-13 14:26:24 +0100 | [diff] [blame] | 640 | #ifdef __i386__ |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 641 | |
Patrik Stridvall | 54fe838 | 2000-04-06 20:21:16 +0000 | [diff] [blame] | 642 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 643 | * SMapLS (KERNEL32.@) |
Patrik Stridvall | 54fe838 | 2000-04-06 20:21:16 +0000 | [diff] [blame] | 644 | */ |
Alexandre Julliard | 643463b | 2006-01-13 14:26:24 +0100 | [diff] [blame] | 645 | __ASM_GLOBAL_FUNC( SMapLS, |
| 646 | "xor %edx,%edx\n\t" |
| 647 | "testl $0xffff0000,%eax\n\t" |
| 648 | "jz 1f\n\t" |
| 649 | "pushl %eax\n\t" |
| 650 | "call " __ASM_NAME("MapLS") "\n\t" |
| 651 | "movl %eax,%edx\n" |
| 652 | "1:\tret" ); |
Alexandre Julliard | 670cdc4 | 1997-08-24 16:00:30 +0000 | [diff] [blame] | 653 | |
Patrik Stridvall | 54fe838 | 2000-04-06 20:21:16 +0000 | [diff] [blame] | 654 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 655 | * SUnMapLS (KERNEL32.@) |
Patrik Stridvall | 54fe838 | 2000-04-06 20:21:16 +0000 | [diff] [blame] | 656 | */ |
Alexandre Julliard | 643463b | 2006-01-13 14:26:24 +0100 | [diff] [blame] | 657 | __ASM_GLOBAL_FUNC( SUnMapLS, |
| 658 | "pushl %eax\n\t" /* preserve eax */ |
| 659 | "pushl %eax\n\t" |
| 660 | "call " __ASM_NAME("UnMapLS") "\n\t" |
| 661 | "popl %eax\n\t" |
| 662 | "ret" ); |
Alexandre Julliard | 2c7a248 | 2005-05-07 18:25:51 +0000 | [diff] [blame] | 663 | |
| 664 | /*********************************************************************** |
| 665 | * SMapLS_IP_EBP_8 (KERNEL32.@) |
| 666 | * SMapLS_IP_EBP_12 (KERNEL32.@) |
| 667 | * SMapLS_IP_EBP_16 (KERNEL32.@) |
| 668 | * SMapLS_IP_EBP_20 (KERNEL32.@) |
| 669 | * SMapLS_IP_EBP_24 (KERNEL32.@) |
| 670 | * SMapLS_IP_EBP_28 (KERNEL32.@) |
| 671 | * SMapLS_IP_EBP_32 (KERNEL32.@) |
| 672 | * SMapLS_IP_EBP_36 (KERNEL32.@) |
| 673 | * SMapLS_IP_EBP_40 (KERNEL32.@) |
| 674 | * |
| 675 | * These functions map linear pointers at [EBP+xxx] to segmented pointers |
| 676 | * and return them. |
| 677 | * Win95 uses some kind of alias structs, which it stores in [EBP+x] to |
| 678 | * unravel them at SUnMapLS. We just store the segmented pointer there. |
| 679 | */ |
| 680 | #define DEFINE_SMapLS(n) \ |
Alexandre Julliard | 643463b | 2006-01-13 14:26:24 +0100 | [diff] [blame] | 681 | __ASM_GLOBAL_FUNC( SMapLS_IP_EBP_ ## n, \ |
| 682 | "movl " #n "(%ebp),%eax\n\t" \ |
| 683 | "call " __ASM_NAME("SMapLS") "\n\t" \ |
| 684 | "movl %edx," #n "(%ebp)\n\t" \ |
| 685 | "ret" ); |
Alexandre Julliard | 2c7a248 | 2005-05-07 18:25:51 +0000 | [diff] [blame] | 686 | |
| 687 | DEFINE_SMapLS(8); |
| 688 | DEFINE_SMapLS(12); |
| 689 | DEFINE_SMapLS(16); |
| 690 | DEFINE_SMapLS(20); |
| 691 | DEFINE_SMapLS(24); |
| 692 | DEFINE_SMapLS(28); |
| 693 | DEFINE_SMapLS(32); |
| 694 | DEFINE_SMapLS(36); |
| 695 | DEFINE_SMapLS(40); |
| 696 | |
Patrik Stridvall | 54fe838 | 2000-04-06 20:21:16 +0000 | [diff] [blame] | 697 | |
| 698 | /*********************************************************************** |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 699 | * SUnMapLS_IP_EBP_8 (KERNEL32.@) |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 700 | * SUnMapLS_IP_EBP_12 (KERNEL32.@) |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 701 | * SUnMapLS_IP_EBP_16 (KERNEL32.@) |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 702 | * SUnMapLS_IP_EBP_20 (KERNEL32.@) |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 703 | * SUnMapLS_IP_EBP_24 (KERNEL32.@) |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 704 | * SUnMapLS_IP_EBP_28 (KERNEL32.@) |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 705 | * SUnMapLS_IP_EBP_32 (KERNEL32.@) |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 706 | * SUnMapLS_IP_EBP_36 (KERNEL32.@) |
Patrik Stridvall | dae8de6 | 2001-06-13 20:13:18 +0000 | [diff] [blame] | 707 | * SUnMapLS_IP_EBP_40 (KERNEL32.@) |
Patrik Stridvall | 54fe838 | 2000-04-06 20:21:16 +0000 | [diff] [blame] | 708 | */ |
Alexandre Julliard | 2c7a248 | 2005-05-07 18:25:51 +0000 | [diff] [blame] | 709 | |
| 710 | #define DEFINE_SUnMapLS(n) \ |
Alexandre Julliard | 643463b | 2006-01-13 14:26:24 +0100 | [diff] [blame] | 711 | __ASM_GLOBAL_FUNC( SUnMapLS_IP_EBP_ ## n, \ |
| 712 | "pushl %eax\n\t" /* preserve eax */ \ |
| 713 | "pushl " #n "(%ebp)\n\t" \ |
| 714 | "call " __ASM_NAME("UnMapLS") "\n\t" \ |
| 715 | "movl $0," #n "(%ebp)\n\t" \ |
| 716 | "popl %eax\n\t" \ |
| 717 | "ret" ) |
Alexandre Julliard | 2c7a248 | 2005-05-07 18:25:51 +0000 | [diff] [blame] | 718 | |
| 719 | DEFINE_SUnMapLS(8); |
| 720 | DEFINE_SUnMapLS(12); |
| 721 | DEFINE_SUnMapLS(16); |
| 722 | DEFINE_SUnMapLS(20); |
| 723 | DEFINE_SUnMapLS(24); |
| 724 | DEFINE_SUnMapLS(28); |
| 725 | DEFINE_SUnMapLS(32); |
| 726 | DEFINE_SUnMapLS(36); |
| 727 | DEFINE_SUnMapLS(40); |
| 728 | |
Alexandre Julliard | 643463b | 2006-01-13 14:26:24 +0100 | [diff] [blame] | 729 | #endif /* __i386__ */ |