]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/mk_timer.c
xnu-3789.51.2.tar.gz
[apple/xnu.git] / osfmk / kern / mk_timer.c
index af9e1c2c0f89b533faa2982e503aab655e5766ac..7ca764dac6a03e950a4bdc7fdc78b553e628bc96 100644 (file)
@@ -1,31 +1,29 @@
 /*
  * Copyright (c) 2000-2004 Apple Computer, 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@
  */
 /*
  * Copyright (c) 2000 Apple Computer, Inc.  All rights reserved.
@@ -83,7 +81,7 @@ mk_timer_create_trap(
        }
 
        simple_lock_init(&timer->lock, 0);
-       call_entry_setup(&timer->call_entry, mk_timer_expire, timer);
+       thread_call_setup(&timer->call_entry, mk_timer_expire, timer);
        timer->is_armed = timer->is_dead = FALSE;
        timer->active = 0;
 
@@ -139,6 +137,8 @@ mk_timer_init(void)
        assert(!(mk_timer_zone != NULL));
 
        mk_timer_zone = zinit(s, (4096 * s), (16 * s), "mk_timer");
+
+       zone_change(mk_timer_zone, Z_NOENCRYPT, TRUE);
 }
 
 static void
@@ -167,14 +167,16 @@ mk_timer_expire(
                timer->is_armed = FALSE;
                simple_unlock(&timer->lock);
 
-               msg.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0);
+               msg.header.msgh_bits =
+                   MACH_MSGH_BITS_SET(MACH_MSG_TYPE_COPY_SEND, 0, 0, 0);
                msg.header.msgh_remote_port = port;
                msg.header.msgh_local_port = MACH_PORT_NULL;
-               msg.header.msgh_reserved = msg.header.msgh_id = 0;
+               msg.header.msgh_voucher_port = MACH_PORT_NULL;
+               msg.header.msgh_id = 0;
 
                msg.unused[0] = msg.unused[1] = msg.unused[2] = 0;
 
-               (void) mach_msg_send_from_kernel(&msg.header, sizeof (msg));
+               (void) mach_msg_send_from_kernel_proper(&msg.header, sizeof (msg));
 
                simple_lock(&timer->lock);
        }
@@ -190,6 +192,18 @@ mk_timer_expire(
        simple_unlock(&timer->lock);
 }
 
+/*
+ * mk_timer_destroy_trap: Destroy the Mach port associated with a timer
+ *
+ * Parameters:  args                     User argument descriptor (see below)
+ *
+ * Indirect:     args->name               Mach port name
+ *
+ *
+ * Returns:        0                      Success
+ *                !0                      Not success           
+ *
+ */
 kern_return_t
 mk_timer_destroy_trap(
        struct mk_timer_destroy_trap_args *args)
@@ -215,16 +229,26 @@ mk_timer_destroy_trap(
        return (result);
 }
 
-kern_return_t
-mk_timer_arm_trap(
-       struct mk_timer_arm_trap_args *args)
-{
-       mach_port_name_t        name = args->name;
-       uint64_t                        expire_time = args->expire_time;
+/*
+ * mk_timer_arm_trap: Start (arm) a timer
+ *
+ * Parameters:  args                     User argument descriptor (see below)
+ *
+ * Indirect:     args->name               Mach port name
+ *               args->expire_time        Time when timer expires
+ *
+ *
+ * Returns:        0                      Success
+ *                !0                      Not success           
+ *
+ */
+
+static kern_return_t
+mk_timer_arm_trap_internal(mach_port_name_t name, uint64_t expire_time, uint64_t mk_leeway, uint64_t mk_timer_flags) {
        mk_timer_t                      timer;
        ipc_space_t                     myspace = current_space();
        ipc_port_t                      port;
-       kern_return_t           result;
+       kern_return_t                   result;
 
        result = ipc_port_translate_receive(myspace, name, &port);
        if (result != KERN_SUCCESS)
@@ -233,6 +257,7 @@ mk_timer_arm_trap(
        if (ip_kotype(port) == IKOT_TIMER) {
                timer = (mk_timer_t)port->ip_kobject;
                assert(timer != NULL);
+
                simple_lock(&timer->lock);
                assert(timer->port == port);
                ip_unlock(port);
@@ -240,20 +265,60 @@ mk_timer_arm_trap(
                if (!timer->is_dead) {
                        timer->is_armed = TRUE;
 
-                       if (!thread_call_enter_delayed(&timer->call_entry, expire_time))
-                               timer->active++;
+                       if (expire_time > mach_absolute_time()) {
+                               uint32_t tcflags = THREAD_CALL_DELAY_USER_NORMAL;
+
+                               if (mk_timer_flags & MK_TIMER_CRITICAL) {
+                                       tcflags = THREAD_CALL_DELAY_USER_CRITICAL;
+                               }
+
+                               if (mk_leeway != 0) {
+                                       tcflags |= THREAD_CALL_DELAY_LEEWAY;
+                               }
+
+                               if (!thread_call_enter_delayed_with_leeway(
+                                       &timer->call_entry, NULL,
+                                       expire_time, mk_leeway, tcflags)) {
+
+                                       timer->active++;
+                               }
+                       } else {
+                               if (!thread_call_enter1(&timer->call_entry, NULL))
+                                       timer->active++;
+                       }
                }
 
                simple_unlock(&timer->lock);
-       }
-       else {
+       } else {
                ip_unlock(port);
                result = KERN_INVALID_ARGUMENT;
        }
-
        return (result);
 }
 
+kern_return_t
+mk_timer_arm_trap(struct mk_timer_arm_trap_args *args) {
+       return mk_timer_arm_trap_internal(args->name, args->expire_time, 0, MK_TIMER_NORMAL);
+}
+
+kern_return_t
+mk_timer_arm_leeway_trap(struct mk_timer_arm_leeway_trap_args *args) {
+       return mk_timer_arm_trap_internal(args->name, args->expire_time, args->mk_leeway, args->mk_timer_flags);
+}
+
+/*
+ * mk_timer_cancel_trap: Cancel a timer
+ *
+ * Parameters:  args                     User argument descriptor (see below)
+ *
+ * Indirect:     args->name               Mach port name
+ *               args->result_time        The armed time of the cancelled timer (return value)
+ *
+ *
+ * Returns:        0                      Success
+ *                !0                      Not success           
+ *
+ */
 kern_return_t
 mk_timer_cancel_trap(
        struct mk_timer_cancel_trap_args *args)
@@ -278,7 +343,7 @@ mk_timer_cancel_trap(
                ip_unlock(port);
 
                if (timer->is_armed) {
-                       armed_time = timer->call_entry.deadline;
+                       armed_time = timer->call_entry.tc_call.deadline;
                        if (thread_call_cancel(&timer->call_entry))
                                timer->active--;
                        timer->is_armed = FALSE;