]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/chud/ppc/chud_thread_ppc.c
xnu-1456.1.26.tar.gz
[apple/xnu.git] / osfmk / chud / ppc / chud_thread_ppc.c
index b7d503e55156b05cd57052f3fd17d980bf3d734d..0bca0ac92cc0c676bd1acbc4c7c6ed2627acbd0e 100644 (file)
@@ -1,31 +1,29 @@
 /*
- * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2003-2007 Apple Inc. All rights reserved.
  *
- * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
- * This file contains Original Code and/or Modifications of Original Code 
- * as defined in and that are subject to the Apple Public Source License 
- * Version 2.0 (the 'License'). You may not use this file except in 
- * compliance with the License.  The rights granted to you under the 
- * License may not be used to create, or enable the creation or 
- * redistribution of, unlawful or unlicensed copies of an Apple operating 
- * system, or to circumvent, violate, or enable the circumvention or 
- * violation of, any terms of an Apple operating system software license 
- * agreement.
- *
- * Please obtain a copy of the License at 
- * http://www.opensource.apple.com/apsl/ and read it before using this 
- * file.
- *
- * The Original Code and all software distributed under the License are 
- * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
- * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
- * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
- * Please see the License for the specific language governing rights and 
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
+ * 
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
  * limitations under the License.
- *
- * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
+ * 
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 
 #include <mach/mach_types.h>
 #include <ppc/machine_routines.h>
 #include <ppc/fpu_protos.h>
 
-// forward declarations
-extern kern_return_t machine_thread_get_kern_state( thread_t                thread,
-                                                                                                       thread_flavor_t         flavor,
-                                                                                                       thread_state_t          tstate,
-                                                                                                       mach_msg_type_number_t  *count);
-
-
+#if 0
 #pragma mark **** thread state ****
+#endif
 
 __private_extern__
 kern_return_t chudxnu_copy_savearea_to_threadstate(thread_flavor_t flavor, thread_state_t tstate, mach_msg_type_number_t *count, struct savearea *sv)
@@ -336,12 +329,19 @@ kern_return_t chudxnu_thread_set_state(thread_t thread,
     }
 }
 
+#if 0
 #pragma mark **** task memory read/write ****
+#endif
     
 __private_extern__
 kern_return_t chudxnu_task_read(task_t task, void *kernaddr, uint64_t usraddr, vm_size_t size)
 {
     kern_return_t ret = KERN_SUCCESS;
+
+       if(ml_at_interrupt_context()) {
+               // can't do this on an interrupt stack
+               return KERN_FAILURE;
+       }
     
        if(!chudxnu_is_64bit_task(task)) { // clear any cruft out of upper 32-bits for 32-bit tasks
                usraddr &= 0x00000000FFFFFFFFULL;
@@ -351,15 +351,13 @@ kern_return_t chudxnu_task_read(task_t task, void *kernaddr, uint64_t usraddr, v
                thread_t      cur_thr = current_thread();
                vm_offset_t   recover_handler = cur_thr->recover; 
                
-               if(ml_at_interrupt_context()) {
-                       return KERN_FAILURE; // can't do copyin on interrupt stack
-               }
-       
                if(copyin(usraddr, kernaddr, size)) {
                        ret = KERN_FAILURE;
                }
+
                cur_thr->recover = recover_handler;
     } else {
+               
                vm_map_t map = get_task_map(task);
                ret = vm_map_read_user(map, usraddr, kernaddr, size);
     }
@@ -371,7 +369,12 @@ __private_extern__
 kern_return_t chudxnu_task_write(task_t task, uint64_t useraddr, void *kernaddr, vm_size_t size)
 {
     kern_return_t ret = KERN_SUCCESS;
-    
+       if(ml_at_interrupt_context()) {
+               // can't do this on an interrupt stack
+               return KERN_FAILURE;
+       }
+
        if(!chudxnu_is_64bit_task(task)) { // clear any cruft out of upper 32-bits for 32-bit tasks
                useraddr &= 0x00000000FFFFFFFFULL;
        }
@@ -380,15 +383,12 @@ kern_return_t chudxnu_task_write(task_t task, uint64_t useraddr, void *kernaddr,
                thread_t      cur_thr = current_thread();
                vm_offset_t   recover_handler = cur_thr->recover; 
                                        
-               if(ml_at_interrupt_context()) {
-                       return KERN_FAILURE; // can't do copyout on interrupt stack
-               }
-       
                if(copyout(kernaddr, useraddr, size)) {
                        ret = KERN_FAILURE;
                }
                cur_thr->recover = recover_handler;
     } else {
+               
                vm_map_t map = get_task_map(task);
                ret = vm_map_write_user(map, kernaddr, useraddr, size);
     }          
@@ -399,76 +399,15 @@ kern_return_t chudxnu_task_write(task_t task, uint64_t useraddr, void *kernaddr,
 __private_extern__
 kern_return_t chudxnu_kern_read(void *dstaddr, vm_offset_t srcaddr, vm_size_t size)
 {
-    while(size>0) {
-               ppnum_t pp;
-               addr64_t phys_addr;    
-               
-               pp = pmap_find_phys(kernel_pmap, srcaddr);                      /* Get the page number */
-               if(!pp) {
-                       return KERN_FAILURE;                                    /* Not mapped... */
-               }
-               
-               phys_addr = ((addr64_t)pp << 12) | (srcaddr & 0x0000000000000FFFULL);   /* Shove in the page offset */
-               if(phys_addr >= mem_actual) {
-                       return KERN_FAILURE;                                    /* out of range */
-               }
-               
-               if((phys_addr&0x1) || size==1) {
-                       *((uint8_t *)dstaddr) = ml_phys_read_byte_64(phys_addr);
-                       ((uint8_t *)dstaddr)++;
-                       srcaddr += sizeof(uint8_t);
-                       size -= sizeof(uint8_t);
-               } else if((phys_addr&0x3) || size<=2) {
-                       *((uint16_t *)dstaddr) = ml_phys_read_half_64(phys_addr);
-                       ((uint16_t *)dstaddr)++;
-                       srcaddr += sizeof(uint16_t);
-                       size -= sizeof(uint16_t);
-               } else {
-                       *((uint32_t *)dstaddr) = ml_phys_read_word_64(phys_addr);
-                       ((uint32_t *)dstaddr)++;
-                       srcaddr += sizeof(uint32_t);
-                       size -= sizeof(uint32_t);
-               }
-    }
-    return KERN_SUCCESS;
+       return (ml_nofault_copy(srcaddr, (vm_offset_t) dstaddr, size) == size ?
+           KERN_SUCCESS: KERN_FAILURE);
 }
 
 __private_extern__
-kern_return_t chudxnu_kern_write(vm_offset_t dstaddr, void *srcaddr, vm_size_t size)
+kern_return_t chudxnu_kern_write(vm_offset_t dstaddr, void *srcaddr, vm_size_t size)
 {
-    while(size>0) {
-               ppnum_t pp;
-               addr64_t phys_addr;    
-               
-               pp = pmap_find_phys(kernel_pmap, dstaddr);                      /* Get the page number */
-               if(!pp) {
-                       return KERN_FAILURE;                                    /* Not mapped... */
-               }
-               
-               phys_addr = ((addr64_t)pp << 12) | (dstaddr & 0x0000000000000FFFULL);   /* Shove in the page offset */
-               if(phys_addr >= mem_actual) {
-                       return KERN_FAILURE;                                    /* out of range */
-               }
-               
-               if((phys_addr&0x1) || size==1) {
-                       ml_phys_write_byte_64(phys_addr, *((uint8_t *)srcaddr));
-                       ((uint8_t *)srcaddr)++;
-                       dstaddr += sizeof(uint8_t);
-                       size -= sizeof(uint8_t);
-               } else if((phys_addr&0x3) || size<=2) {
-                       ml_phys_write_half_64(phys_addr, *((uint16_t *)srcaddr));
-                       ((uint16_t *)srcaddr)++;
-                       dstaddr += sizeof(uint16_t);
-                       size -= sizeof(uint16_t);
-               } else {
-                       ml_phys_write_word_64(phys_addr, *((uint32_t *)srcaddr));
-                       ((uint32_t *)srcaddr)++;
-                       dstaddr += sizeof(uint32_t);
-                       size -= sizeof(uint32_t);
-               }
-    }
-    
-    return KERN_SUCCESS;
+       return (ml_nofault_copy((vm_offset_t) srcaddr, dstaddr, size) == size ?
+           KERN_SUCCESS: KERN_FAILURE);
 }
 
 // chudxnu_thread_get_callstack gathers a raw callstack along with any information needed to
@@ -515,8 +454,8 @@ kern_return_t chudxnu_thread_get_callstack64(       thread_t thread,
     uint64_t currPC, currLR, currR0;
     uint64_t framePointer;
     uint64_t prevPC = 0;
-    uint64_t kernStackMin = min_valid_stack_address();
-    uint64_t kernStackMax = max_valid_stack_address();
+    uint64_t kernStackMin = thread->kernel_stack;
+    uint64_t kernStackMax = kernStackMin + kernel_stack_size;
     uint64_t *buffer = callStack;
     uint32_t tmpWord;
     int bufferIndex = 0;
@@ -538,8 +477,7 @@ kern_return_t chudxnu_thread_get_callstack64(       thread_t thread,
 
     supervisor = SUPERVISOR_MODE(sv->save_srr1);
     if(supervisor) {
-#warning assuming kernel task is always 32-bit
-               is64Bit = FALSE;
+               is64Bit = FALSE; /* XXX assuming task is always 32-bit */
     } else {
                is64Bit = chudxnu_is_64bit_task(task);
     }
@@ -646,176 +584,3 @@ kern_return_t chudxnu_thread_get_callstack64(     thread_t thread,
     return KERN_SUCCESS;
 }
 
-__private_extern__
-kern_return_t chudxnu_thread_get_callstack( thread_t thread, 
-                                           uint32_t *callStack,
-                                           mach_msg_type_number_t *count,
-                                           boolean_t user_only)
-{
-    kern_return_t kr;
-    task_t task = get_threadtask(thread);
-    uint64_t nextFramePointer = 0;
-    uint64_t currPC, currLR, currR0;
-    uint64_t framePointer;
-    uint64_t prevPC = 0;
-    uint64_t kernStackMin = min_valid_stack_address();
-    uint64_t kernStackMax = max_valid_stack_address();
-    uint32_t *buffer = callStack;
-    uint32_t tmpWord;
-    int bufferIndex = 0;
-    int bufferMaxIndex = *count;
-    boolean_t supervisor;
-    boolean_t is64Bit;
-    struct savearea *sv;
-
-    if(user_only) {
-        sv = find_user_regs(thread);
-    } else {
-        sv = find_kern_regs(thread);
-    }
-
-    if(!sv) {
-        *count = 0;
-        return KERN_FAILURE;
-    }
-
-    supervisor = SUPERVISOR_MODE(sv->save_srr1);
-    if(supervisor) {
-#warning assuming kernel task is always 32-bit
-               is64Bit = FALSE;
-    } else {
-               is64Bit = chudxnu_is_64bit_task(task);
-    }
-
-    bufferMaxIndex = bufferMaxIndex - 2; // allot space for saving the LR and R0 on the stack at the end.
-    if(bufferMaxIndex<2) {
-        *count = 0;
-        return KERN_RESOURCE_SHORTAGE;
-    }
-
-    currPC = sv->save_srr0;
-    framePointer = sv->save_r1; /* r1 is the stack pointer (no FP on PPC)  */
-    currLR = sv->save_lr;
-    currR0 = sv->save_r0;
-
-    bufferIndex = 0;  // start with a stack of size zero
-    buffer[bufferIndex++] = currPC; // save PC in position 0.
-
-    // Now, fill buffer with stack backtraces.
-    while(bufferIndex<bufferMaxIndex && VALID_STACK_ADDRESS(framePointer)) {
-        uint64_t pc = 0;
-        // Above the stack pointer, the following values are saved:
-        // saved LR
-        // saved CR
-        // saved SP
-        //-> SP
-        // Here, we'll get the lr from the stack.
-        uint64_t fp_link;
-
-               if(is64Bit) {
-                       fp_link = framePointer + FP_LINK_OFFSET*sizeof(uint64_t);
-               } else {
-                       fp_link = framePointer + FP_LINK_OFFSET*sizeof(uint32_t);
-               }
-
-        // Note that we read the pc even for the first stack frame (which, in theory,
-        // is always empty because the callee fills it in just before it lowers the
-        // stack.  However, if we catch the program in between filling in the return
-        // address and lowering the stack, we want to still have a valid backtrace.
-        // FixupStack correctly disregards this value if necessary.
-
-        if(supervisor) {
-                       if(is64Bit) {
-                               kr = chudxnu_kern_read(&pc, fp_link, sizeof(uint64_t));
-                       } else {
-                               kr = chudxnu_kern_read(&tmpWord, fp_link, sizeof(uint32_t));
-                               pc = tmpWord;
-                       }    
-        } else {
-                       if(is64Bit) {
-                               kr = chudxnu_task_read(task, &pc, fp_link, sizeof(uint64_t));
-                       } else {
-                               kr = chudxnu_task_read(task, &tmpWord, fp_link, sizeof(uint32_t));
-                               pc = tmpWord;
-                       }
-        }
-        if(kr!=KERN_SUCCESS) {
-            pc = 0;
-            break;
-        }
-
-        // retrieve the contents of the frame pointer and advance to the next stack frame if it's valid
-        if(supervisor) {
-                       if(is64Bit) {
-                               kr = chudxnu_kern_read(&nextFramePointer, framePointer, sizeof(uint64_t));
-                       } else {
-                               kr = chudxnu_kern_read(&tmpWord, framePointer, sizeof(uint32_t));
-                               nextFramePointer = tmpWord;
-                       }  
-        } else {
-                       if(is64Bit) {
-                               kr = chudxnu_task_read(task, &nextFramePointer, framePointer, sizeof(uint64_t));
-                       } else {
-                               kr = chudxnu_task_read(task, &tmpWord, framePointer, sizeof(uint32_t));
-                               nextFramePointer = tmpWord;
-                       }
-        }
-        if(kr!=KERN_SUCCESS) {
-            nextFramePointer = 0;
-        }
-
-        if(nextFramePointer) {
-            buffer[bufferIndex++] = pc;
-            prevPC = pc;
-        }
-    
-        if(nextFramePointer<framePointer) {
-            break;
-        } else {
-               framePointer = nextFramePointer;
-               }
-    }
-
-    if(bufferIndex>=bufferMaxIndex) {
-        *count = 0;
-        return KERN_RESOURCE_SHORTAGE;
-    }
-
-    // Save link register and R0 at bottom of stack (used for later fixup).
-    buffer[bufferIndex++] = currLR;
-    buffer[bufferIndex++] = currR0;
-
-    *count = bufferIndex;
-    return KERN_SUCCESS;
-}
-
-#pragma mark **** DEPRECATED ****
-
-// DEPRECATED
-__private_extern__
-kern_return_t chudxnu_bind_current_thread(int cpu)
-{
-       return chudxnu_bind_thread(current_thread(), cpu);
-}
-
-// DEPRECATED
-kern_return_t chudxnu_unbind_current_thread(void)
-{
-       return chudxnu_unbind_thread(current_thread());
-}
-
-// DEPRECATED
-__private_extern__
-kern_return_t chudxnu_current_thread_get_callstack(    uint32_t *callStack,
-                                                                                                       mach_msg_type_number_t *count,
-                                                                                                       boolean_t user_only)
-{
-       return chudxnu_thread_get_callstack(current_thread(), callStack, count, user_only);
-}
-
-// DEPRECATED
-__private_extern__
-thread_t chudxnu_current_act(void)
-{
-       return chudxnu_current_thread();
-}