Mercurial > hg > xemacs-beta
annotate src/vdb-mach.c @ 5486:58e320bde005
Handle redisplay edge case.
With motion events when entering a frame and the minibuffer is
active, row and column can be zero, and there aren't any runes.
| author | Mike Kupfer <mike.kupfer@xemacs.org> |
|---|---|
| date | Sat, 30 Apr 2011 13:30:47 +0900 |
| parents | 308d34e9f07d |
| children |
| rev | line source |
|---|---|
| 3092 | 1 /* Virtual diry bit implementation for XEmacs. |
| 2 Copyright (C) 2005 Marcus Crestani. | |
| 3 | |
| 4 This file is part of XEmacs. | |
| 5 | |
|
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
3092
diff
changeset
|
6 XEmacs is free software: you can redistribute it and/or modify it |
| 3092 | 7 under the terms of the GNU General Public License as published by the |
|
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
3092
diff
changeset
|
8 Free Software Foundation, either version 3 of the License, or (at your |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
3092
diff
changeset
|
9 option) any later version. |
| 3092 | 10 |
| 11 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
| 12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 for more details. | |
| 15 | |
| 16 You should have received a copy of the GNU General Public License | |
|
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
3092
diff
changeset
|
17 along with XEmacs. If not, see <http://www.gnu.org/licenses/>. */ |
| 3092 | 18 |
| 19 /* Synched up with: Not in FSF. */ | |
| 20 | |
| 21 #include <config.h> | |
| 22 #include "lisp.h" | |
| 23 #include "gc.h" | |
| 24 #include "mc-alloc.h" | |
| 25 #include "vdb.h" | |
| 26 | |
| 27 #include <errno.h> | |
| 28 #include <signal.h> | |
| 29 #include <sys/mman.h> | |
| 30 #include <sys/time.h> | |
| 31 #include <sys/resource.h> | |
| 32 #include <unistd.h> | |
| 33 #include <mach/mach.h> | |
| 34 #include <mach/mach_error.h> | |
| 35 #include <architecture/ppc/cframe.h> | |
| 36 | |
| 37 | |
| 38 /* the structure of an exception msg and its reply */ | |
| 39 typedef struct rep_msg { | |
| 40 mach_msg_header_t head; | |
| 41 NDR_record_t NDR; | |
| 42 kern_return_t ret_code; | |
| 43 } mach_reply_msg_t; | |
| 44 | |
| 45 typedef struct exc_msg { | |
| 46 mach_msg_header_t head; | |
| 47 /* start of the kernel processed data */ | |
| 48 mach_msg_body_t msgh_body; | |
| 49 mach_msg_port_descriptor_t thread; | |
| 50 mach_msg_port_descriptor_t task; | |
| 51 /* end of the kernel processed data */ | |
| 52 NDR_record_t NDR; | |
| 53 exception_type_t exception; | |
| 54 mach_msg_type_number_t code_cnt; | |
| 55 exception_data_t code; | |
| 56 /* some padding */ | |
| 57 char pad[512]; | |
| 58 } mach_exc_msg_t; | |
| 59 | |
| 60 /* this is a neat little mach callback */ | |
| 61 extern boolean_t exc_server(mach_msg_header_t *in, mach_msg_header_t *out); | |
| 62 | |
| 63 /* these are the globals everyone needs */ | |
| 64 static size_t page_size = 16384; | |
| 65 static mach_port_t task_self = NULL; | |
| 66 static mach_port_t exc_port = NULL; | |
| 67 | |
| 68 /* these are some less neat mach callbacks */ | |
| 69 kern_return_t | |
| 70 catch_exception_raise_state | |
| 71 (mach_port_t UNUSED (port), | |
| 72 exception_type_t UNUSED (exception_type), | |
| 73 exception_data_t UNUSED (exception_data), | |
| 74 mach_msg_type_number_t UNUSED (data_cnt), | |
| 75 thread_state_flavor_t *UNUSED (flavor), | |
| 76 thread_state_t UNUSED (in_state), | |
| 77 mach_msg_type_number_t UNUSED (is_cnt), | |
| 78 thread_state_t UNUSED (out_state), | |
| 79 mach_msg_type_number_t UNUSED (os_cnt)) | |
| 80 { | |
| 81 return KERN_FAILURE; | |
| 82 } | |
| 83 | |
| 84 kern_return_t | |
| 85 catch_exception_raise_state_identitity | |
| 86 (mach_port_t UNUSED (port), | |
| 87 mach_port_t UNUSED (thread_port), | |
| 88 mach_port_t UNUSED (task_port), | |
| 89 exception_type_t UNUSED (exception_type), | |
| 90 exception_data_t UNUSED (exception_data), | |
| 91 mach_msg_type_number_t UNUSED (data_count), | |
| 92 thread_state_flavor_t *UNUSED (state_flavor), | |
| 93 thread_state_t UNUSED (in_state), | |
| 94 mach_msg_type_number_t UNUSED (in_state_count), | |
| 95 thread_state_t UNUSED (out_state), | |
| 96 mach_msg_type_number_t UNUSED (out_state_count)) | |
| 97 { | |
| 98 return KERN_FAILURE; | |
| 99 } | |
| 100 | |
| 101 kern_return_t | |
| 102 catch_exception_raise | |
| 103 (mach_port_t UNUSED (port), | |
| 104 mach_port_t UNUSED (thread_port), | |
| 105 mach_port_t UNUSED (task_port), | |
| 106 exception_type_t UNUSED (exception_type), | |
| 107 exception_data_t exception_data, | |
| 108 mach_msg_type_number_t UNUSED (data_count)) | |
| 109 { | |
| 110 /* kernel return value is in exception_data[0], faulting address in | |
| 111 exception_data[1] */ | |
| 112 if (write_barrier_enabled | |
| 113 && (fault_on_protected_page ((void *) exception_data[1])) | |
| 114 && exception_data[0] == KERN_PROTECTION_FAILURE) | |
| 115 { | |
| 116 vdb_designate_modified ((void *) exception_data[1]); | |
| 117 unprotect_page_and_mark_dirty ((void *) exception_data[1]); | |
| 118 return KERN_SUCCESS; | |
| 119 } | |
| 120 else /* default sigsegv handler */ | |
| 121 { | |
| 122 fprintf (stderr, "\n\nFatal Error: Received %s (%d) for address 0x%x\n", | |
| 123 "EXC_BAD_ACCESS", exception_data[0], (int) exception_data[1]); | |
| 124 return KERN_FAILURE; | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 /* this is the thread which forwards of exceptions read from the exception | |
| 129 server off to our exception catchers and then back out to the other | |
| 130 thread */ | |
| 131 void | |
| 132 exception_thread(void) | |
| 133 { | |
| 134 mach_msg_header_t *message; | |
| 135 mach_msg_header_t *reply; | |
| 136 kern_return_t retval; | |
| 137 | |
| 138 /* allocate the space for the message and reply */ | |
| 139 message = (mach_msg_header_t *) malloc (sizeof (mach_exc_msg_t)); | |
| 140 reply = (mach_msg_header_t *) malloc (sizeof (mach_reply_msg_t)); | |
| 141 /* do this loop forever */ | |
| 142 while (1) | |
| 143 { | |
| 144 /* block until we get an exception message */ | |
| 145 retval = mach_msg (message, MACH_RCV_MSG, 0, sizeof (mach_exc_msg_t), | |
| 146 exc_port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); | |
| 147 /* forward off the handling of this message */ | |
| 148 if (!exc_server (message, reply)) | |
| 149 { | |
| 150 fprintf (stderr, "INTERNAL ERROR: exc_server() failed.\n"); | |
| 151 ABORT (); | |
| 152 } | |
| 153 /* send the message back out to the thread */ | |
| 154 retval = mach_msg (reply, MACH_SEND_MSG, sizeof (mach_reply_msg_t), 0, | |
| 155 MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, | |
| 156 MACH_PORT_NULL); | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 /* this initializes the subsystem (sets the exception port, starts the | |
| 161 exception handling thread, etc) */ | |
| 162 void | |
| 163 vdb_install_signal_handler (void) | |
| 164 { | |
| 165 mach_port_t thread_self, exc_port_s, exc_thread; | |
| 166 ppc_thread_state_t *exc_thread_state; | |
| 167 mach_msg_type_name_t type; | |
| 168 void *subthread_stack; | |
| 169 kern_return_t retval; | |
| 170 | |
| 171 /* get ids for ourself */ | |
| 172 if (!task_self) | |
| 173 task_self = mach_task_self (); | |
| 174 thread_self = mach_thread_self (); | |
| 175 | |
| 176 /* allocate the port we're going to get exceptions on */ | |
| 177 retval = mach_port_allocate (task_self, MACH_PORT_RIGHT_RECEIVE, &exc_port); | |
| 178 if (retval != KERN_SUCCESS) | |
| 179 { | |
| 180 fprintf (stderr, "Couldn't allocate exception port: %s\n", | |
| 181 mach_error_string (retval)); | |
| 182 ABORT (); | |
| 183 } | |
| 184 | |
| 185 /* extract out the send rights for that port, which the OS needs */ | |
| 186 retval = mach_port_extract_right (task_self, exc_port, | |
| 187 MACH_MSG_TYPE_MAKE_SEND, | |
| 188 &exc_port_s, &type); | |
| 189 if(retval != KERN_SUCCESS) | |
| 190 { | |
| 191 fprintf (stderr, "Couldn't extract send rights: %s\n", | |
| 192 mach_error_string (retval)); | |
| 193 ABORT (); | |
| 194 } | |
| 195 | |
| 196 /* set the exception ports for this thread to the above */ | |
| 197 retval = thread_set_exception_ports(thread_self, EXC_MASK_BAD_ACCESS, | |
| 198 exc_port_s, EXCEPTION_DEFAULT, | |
| 199 PPC_THREAD_STATE); | |
| 200 if(retval != KERN_SUCCESS) | |
| 201 { | |
| 202 fprintf (stderr, "Couldn't set exception ports: %s\n", | |
| 203 mach_error_string (retval)); | |
| 204 ABORT (); | |
| 205 } | |
| 206 | |
| 207 /* set up the subthread */ | |
| 208 retval = thread_create(task_self, &exc_thread); | |
| 209 if(retval != KERN_SUCCESS) | |
| 210 { | |
| 211 fprintf (stderr , "Couldn't create exception thread: %s\n", | |
| 212 mach_error_string (retval)); | |
| 213 ABORT (); | |
| 214 } | |
| 215 subthread_stack = (void *) malloc (page_size); | |
| 216 subthread_stack = | |
| 217 (char *) subthread_stack + (page_size - C_ARGSAVE_LEN - C_RED_ZONE); | |
| 218 exc_thread_state = | |
| 219 (ppc_thread_state_t *) malloc (sizeof (ppc_thread_state_t)); | |
| 220 exc_thread_state->srr0 = (unsigned int) exception_thread; | |
| 221 exc_thread_state->r1 = (unsigned int) subthread_stack; | |
| 222 retval = thread_set_state (exc_thread, PPC_THREAD_STATE, | |
| 223 (thread_state_t) exc_thread_state, | |
| 224 PPC_THREAD_STATE_COUNT); | |
| 225 if (retval != KERN_SUCCESS) | |
| 226 { | |
| 227 fprintf (stderr, "Couldn't set subthread state: %s\n", | |
| 228 mach_error_string (retval)); | |
| 229 ABORT (); | |
| 230 } | |
| 231 retval = thread_resume (exc_thread); | |
| 232 if (retval != KERN_SUCCESS) | |
| 233 { | |
| 234 fprintf (stderr, "Couldn't resume subthread: %s\n", | |
| 235 mach_error_string (retval)); | |
| 236 ABORT (); | |
| 237 } | |
| 238 allow_incremental_gc = 1; | |
| 239 } | |
| 240 | |
| 241 void | |
| 242 vdb_protect (void *ptr, EMACS_INT len) | |
| 243 { | |
| 244 if (mprotect (ptr, len, PROT_READ)) | |
| 245 { | |
| 246 perror ("Couldn't mprotect"); | |
| 247 ABORT (); | |
| 248 } | |
| 249 } | |
| 250 | |
| 251 void | |
| 252 vdb_unprotect (void *ptr, EMACS_INT len) | |
| 253 { | |
| 254 if (mprotect (ptr, len, PROT_READ | PROT_WRITE)) | |
| 255 { | |
| 256 perror ("Couldn't mprotect"); | |
| 257 ABORT (); | |
| 258 } | |
| 259 } |
