2 * Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <sys/errno.h>
31 #include <mach/mach_types.h>
32 #include <mach/mach_traps.h>
33 #include <mach/host_priv.h>
34 #include <mach/kern_return.h>
35 #include <mach/memory_object_control.h>
36 #include <mach/memory_object_types.h>
37 #include <mach/port.h>
38 #include <mach/policy.h>
40 #include <mach/thread_act.h>
41 #include <mach/mach_vm.h>
43 #include <kern/host.h>
44 #include <kern/kalloc.h>
45 #include <kern/page_decrypt.h>
46 #include <kern/queue.h>
47 #include <kern/thread.h>
48 #include <kern/ipc_kobject.h>
49 #include <os/refcnt.h>
51 #include <ipc/ipc_port.h>
52 #include <ipc/ipc_space.h>
54 #include <vm/vm_fault.h>
55 #include <vm/vm_map.h>
56 #include <vm/vm_pageout.h>
57 #include <vm/memory_object.h>
58 #include <vm/vm_pageout.h>
59 #include <vm/vm_protos.h>
60 #include <vm/vm_kern.h>
64 * APPLE PROTECT MEMORY PAGER
66 * This external memory manager (EMM) handles memory from the encrypted
67 * sections of some executables protected by the DSMOS kernel extension.
69 * It mostly handles page-in requests (from memory_object_data_request()) by
70 * getting the encrypted data from its backing VM object, itself backed by
71 * the encrypted file, decrypting it and providing it to VM.
73 * The decrypted pages will never be dirtied, so the memory manager doesn't
74 * need to handle page-out requests (from memory_object_data_return()). The
75 * pages need to be mapped copy-on-write, so that the originals stay clean.
77 * We don't expect to have to handle a large number of apple-protected
78 * binaries, so the data structures are very simple (simple linked list)
82 /* forward declarations */
83 void apple_protect_pager_reference(memory_object_t mem_obj
);
84 void apple_protect_pager_deallocate(memory_object_t mem_obj
);
85 kern_return_t
apple_protect_pager_init(memory_object_t mem_obj
,
86 memory_object_control_t control
,
87 memory_object_cluster_size_t pg_size
);
88 kern_return_t
apple_protect_pager_terminate(memory_object_t mem_obj
);
89 kern_return_t
apple_protect_pager_data_request(memory_object_t mem_obj
,
90 memory_object_offset_t offset
,
91 memory_object_cluster_size_t length
,
92 vm_prot_t protection_required
,
93 memory_object_fault_info_t fault_info
);
94 kern_return_t
apple_protect_pager_data_return(memory_object_t mem_obj
,
95 memory_object_offset_t offset
,
96 memory_object_cluster_size_t data_cnt
,
97 memory_object_offset_t
*resid_offset
,
100 boolean_t kernel_copy
,
102 kern_return_t
apple_protect_pager_data_initialize(memory_object_t mem_obj
,
103 memory_object_offset_t offset
,
104 memory_object_cluster_size_t data_cnt
);
105 kern_return_t
apple_protect_pager_data_unlock(memory_object_t mem_obj
,
106 memory_object_offset_t offset
,
107 memory_object_size_t size
,
108 vm_prot_t desired_access
);
109 kern_return_t
apple_protect_pager_synchronize(memory_object_t mem_obj
,
110 memory_object_offset_t offset
,
111 memory_object_size_t length
,
112 vm_sync_t sync_flags
);
113 kern_return_t
apple_protect_pager_map(memory_object_t mem_obj
,
115 kern_return_t
apple_protect_pager_last_unmap(memory_object_t mem_obj
);
117 #define CRYPT_INFO_DEBUG 0
118 void crypt_info_reference(struct pager_crypt_info
*crypt_info
);
119 void crypt_info_deallocate(struct pager_crypt_info
*crypt_info
);
122 * Vector of VM operations for this EMM.
123 * These routines are invoked by VM via the memory_object_*() interfaces.
125 const struct memory_object_pager_ops apple_protect_pager_ops
= {
126 apple_protect_pager_reference
,
127 apple_protect_pager_deallocate
,
128 apple_protect_pager_init
,
129 apple_protect_pager_terminate
,
130 apple_protect_pager_data_request
,
131 apple_protect_pager_data_return
,
132 apple_protect_pager_data_initialize
,
133 apple_protect_pager_data_unlock
,
134 apple_protect_pager_synchronize
,
135 apple_protect_pager_map
,
136 apple_protect_pager_last_unmap
,
137 NULL
, /* data_reclaim */
142 * The "apple_protect_pager" describes a memory object backed by
143 * the "apple protect" EMM.
145 typedef struct apple_protect_pager
{
146 /* mandatory generic header */
147 struct memory_object ap_pgr_hdr
;
149 /* pager-specific data */
150 queue_chain_t pager_queue
; /* next & prev pagers */
151 struct os_refcnt ref_count
; /* reference count */
152 boolean_t is_ready
; /* is this pager ready ? */
153 boolean_t is_mapped
; /* is this mem_obj mapped ? */
154 vm_object_t backing_object
; /* VM obj w/ encrypted data */
155 vm_object_offset_t backing_offset
;
156 vm_object_offset_t crypto_backing_offset
; /* for key... */
157 vm_object_offset_t crypto_start
;
158 vm_object_offset_t crypto_end
;
159 struct pager_crypt_info
*crypt_info
;
160 } *apple_protect_pager_t
;
161 #define APPLE_PROTECT_PAGER_NULL ((apple_protect_pager_t) NULL)
164 * List of memory objects managed by this EMM.
165 * The list is protected by the "apple_protect_pager_lock" lock.
167 int apple_protect_pager_count
= 0; /* number of pagers */
168 int apple_protect_pager_count_mapped
= 0; /* number of unmapped pagers */
169 queue_head_t apple_protect_pager_queue
;
170 decl_lck_mtx_data(, apple_protect_pager_lock
)
173 * Maximum number of unmapped pagers we're willing to keep around.
175 int apple_protect_pager_cache_limit
= 20;
178 * Statistics & counters.
180 int apple_protect_pager_count_max
= 0;
181 int apple_protect_pager_count_unmapped_max
= 0;
182 int apple_protect_pager_num_trim_max
= 0;
183 int apple_protect_pager_num_trim_total
= 0;
186 lck_grp_t apple_protect_pager_lck_grp
;
187 lck_grp_attr_t apple_protect_pager_lck_grp_attr
;
188 lck_attr_t apple_protect_pager_lck_attr
;
191 /* internal prototypes */
192 apple_protect_pager_t
apple_protect_pager_create(
193 vm_object_t backing_object
,
194 vm_object_offset_t backing_offset
,
195 vm_object_offset_t crypto_backing_offset
,
196 struct pager_crypt_info
*crypt_info
,
197 vm_object_offset_t crypto_start
,
198 vm_object_offset_t crypto_end
);
199 apple_protect_pager_t
apple_protect_pager_lookup(memory_object_t mem_obj
);
200 void apple_protect_pager_dequeue(apple_protect_pager_t pager
);
201 void apple_protect_pager_deallocate_internal(apple_protect_pager_t pager
,
203 void apple_protect_pager_terminate_internal(apple_protect_pager_t pager
);
204 void apple_protect_pager_trim(void);
208 int apple_protect_pagerdebug
= 0;
209 #define PAGER_ALL 0xffffffff
210 #define PAGER_INIT 0x00000001
211 #define PAGER_PAGEIN 0x00000002
213 #define PAGER_DEBUG(LEVEL, A) \
215 if ((apple_protect_pagerdebug & LEVEL)==LEVEL) { \
220 #define PAGER_DEBUG(LEVEL, A)
225 apple_protect_pager_bootstrap(void)
227 lck_grp_attr_setdefault(&apple_protect_pager_lck_grp_attr
);
228 lck_grp_init(&apple_protect_pager_lck_grp
, "apple_protect", &apple_protect_pager_lck_grp_attr
);
229 lck_attr_setdefault(&apple_protect_pager_lck_attr
);
230 lck_mtx_init(&apple_protect_pager_lock
, &apple_protect_pager_lck_grp
, &apple_protect_pager_lck_attr
);
231 queue_init(&apple_protect_pager_queue
);
235 * apple_protect_pager_init()
237 * Initialize the memory object and makes it ready to be used and mapped.
240 apple_protect_pager_init(
241 memory_object_t mem_obj
,
242 memory_object_control_t control
,
246 memory_object_cluster_size_t pg_size
)
248 apple_protect_pager_t pager
;
250 memory_object_attr_info_data_t attributes
;
252 PAGER_DEBUG(PAGER_ALL
,
253 ("apple_protect_pager_init: %p, %p, %x\n",
254 mem_obj
, control
, pg_size
));
256 if (control
== MEMORY_OBJECT_CONTROL_NULL
) {
257 return KERN_INVALID_ARGUMENT
;
260 pager
= apple_protect_pager_lookup(mem_obj
);
262 memory_object_control_reference(control
);
264 pager
->ap_pgr_hdr
.mo_control
= control
;
266 attributes
.copy_strategy
= MEMORY_OBJECT_COPY_DELAY
;
267 /* attributes.cluster_size = (1 << (CLUSTER_SHIFT + PAGE_SHIFT));*/
268 attributes
.cluster_size
= (1 << (PAGE_SHIFT
));
269 attributes
.may_cache_object
= FALSE
;
270 attributes
.temporary
= TRUE
;
272 kr
= memory_object_change_attributes(
274 MEMORY_OBJECT_ATTRIBUTE_INFO
,
275 (memory_object_info_t
) &attributes
,
276 MEMORY_OBJECT_ATTR_INFO_COUNT
);
277 if (kr
!= KERN_SUCCESS
) {
278 panic("apple_protect_pager_init: "
279 "memory_object_change_attributes() failed");
282 #if CONFIG_SECLUDED_MEMORY
283 if (secluded_for_filecache
) {
284 memory_object_mark_eligible_for_secluded(control
, TRUE
);
286 #endif /* CONFIG_SECLUDED_MEMORY */
292 * apple_protect_data_return()
294 * Handles page-out requests from VM. This should never happen since
295 * the pages provided by this EMM are not supposed to be dirty or dirtied
296 * and VM should simply discard the contents and reclaim the pages if it
300 apple_protect_pager_data_return(
301 __unused memory_object_t mem_obj
,
302 __unused memory_object_offset_t offset
,
303 __unused memory_object_cluster_size_t data_cnt
,
304 __unused memory_object_offset_t
*resid_offset
,
305 __unused
int *io_error
,
306 __unused boolean_t dirty
,
307 __unused boolean_t kernel_copy
,
308 __unused
int upl_flags
)
310 panic("apple_protect_pager_data_return: should never get called");
315 apple_protect_pager_data_initialize(
316 __unused memory_object_t mem_obj
,
317 __unused memory_object_offset_t offset
,
318 __unused memory_object_cluster_size_t data_cnt
)
320 panic("apple_protect_pager_data_initialize: should never get called");
325 apple_protect_pager_data_unlock(
326 __unused memory_object_t mem_obj
,
327 __unused memory_object_offset_t offset
,
328 __unused memory_object_size_t size
,
329 __unused vm_prot_t desired_access
)
335 * apple_protect_pager_data_request()
337 * Handles page-in requests from VM.
339 int apple_protect_pager_data_request_debug
= 0;
341 apple_protect_pager_data_request(
342 memory_object_t mem_obj
,
343 memory_object_offset_t offset
,
344 memory_object_cluster_size_t length
,
348 vm_prot_t protection_required
,
349 memory_object_fault_info_t mo_fault_info
)
351 apple_protect_pager_t pager
;
352 memory_object_control_t mo_control
;
356 upl_page_info_t
*upl_pl
;
357 unsigned int pl_count
;
358 vm_object_t src_top_object
, src_page_object
, dst_object
;
359 kern_return_t kr
, retval
;
360 vm_offset_t src_vaddr
, dst_vaddr
;
361 vm_offset_t cur_offset
;
362 vm_offset_t offset_in_page
;
363 kern_return_t error_code
;
365 vm_page_t src_page
, top_page
;
367 struct vm_object_fault_info fault_info
;
370 PAGER_DEBUG(PAGER_ALL
, ("apple_protect_pager_data_request: %p, %llx, %x, %x\n", mem_obj
, offset
, length
, protection_required
));
372 retval
= KERN_SUCCESS
;
373 src_top_object
= VM_OBJECT_NULL
;
374 src_page_object
= VM_OBJECT_NULL
;
377 fault_info
= *((struct vm_object_fault_info
*)(uintptr_t)mo_fault_info
);
378 fault_info
.stealth
= TRUE
;
379 fault_info
.io_sync
= FALSE
;
380 fault_info
.mark_zf_absent
= FALSE
;
381 fault_info
.batch_pmap_op
= FALSE
;
382 interruptible
= fault_info
.interruptible
;
384 pager
= apple_protect_pager_lookup(mem_obj
);
385 assert(pager
->is_ready
);
386 assert(os_ref_get_count(&pager
->ref_count
) > 1); /* pager is alive and mapped */
388 PAGER_DEBUG(PAGER_PAGEIN
, ("apple_protect_pager_data_request: %p, %llx, %x, %x, pager %p\n", mem_obj
, offset
, length
, protection_required
, pager
));
390 fault_info
.lo_offset
+= pager
->backing_offset
;
391 fault_info
.hi_offset
+= pager
->backing_offset
;
394 * Gather in a UPL all the VM pages requested by VM.
396 mo_control
= pager
->ap_pgr_hdr
.mo_control
;
400 UPL_RET_ONLY_ABSENT
|
403 UPL_CLEAN_IN_PLACE
| /* triggers UPL_CLEAR_DIRTY */
406 kr
= memory_object_upl_request(mo_control
,
408 &upl
, NULL
, NULL
, upl_flags
, VM_KERN_MEMORY_SECURITY
);
409 if (kr
!= KERN_SUCCESS
) {
413 dst_object
= mo_control
->moc_object
;
414 assert(dst_object
!= VM_OBJECT_NULL
);
417 * We'll map the encrypted data in the kernel address space from the
418 * backing VM object (itself backed by the encrypted file via
421 src_top_object
= pager
->backing_object
;
422 assert(src_top_object
!= VM_OBJECT_NULL
);
423 vm_object_reference(src_top_object
); /* keep the source object alive */
426 * Fill in the contents of the pages requested by VM.
428 upl_pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
429 pl_count
= length
/ PAGE_SIZE
;
431 retval
== KERN_SUCCESS
&& cur_offset
< length
;
432 cur_offset
+= PAGE_SIZE
) {
435 if (!upl_page_present(upl_pl
, (int)(cur_offset
/ PAGE_SIZE
))) {
436 /* this page is not in the UPL: skip it */
441 * Map the source (encrypted) page in the kernel's
442 * virtual address space.
443 * We already hold a reference on the src_top_object.
446 vm_object_lock(src_top_object
);
447 vm_object_paging_begin(src_top_object
);
450 src_page
= VM_PAGE_NULL
;
451 kr
= vm_fault_page(src_top_object
,
452 pager
->backing_offset
+ offset
+ cur_offset
,
455 FALSE
, /* src_page not looked up */
465 case VM_FAULT_SUCCESS
:
468 goto retry_src_fault
;
469 case VM_FAULT_MEMORY_SHORTAGE
:
470 if (vm_page_wait(interruptible
)) {
471 goto retry_src_fault
;
474 case VM_FAULT_INTERRUPTED
:
475 retval
= MACH_SEND_INTERRUPTED
;
477 case VM_FAULT_SUCCESS_NO_VM_PAGE
:
478 /* success but no VM page: fail */
479 vm_object_paging_end(src_top_object
);
480 vm_object_unlock(src_top_object
);
482 case VM_FAULT_MEMORY_ERROR
:
483 /* the page is not there ! */
487 retval
= KERN_MEMORY_ERROR
;
491 panic("apple_protect_pager_data_request: "
492 "vm_fault_page() unexpected error 0x%x\n",
495 assert(src_page
!= VM_PAGE_NULL
);
496 assert(src_page
->vmp_busy
);
498 if (src_page
->vmp_q_state
!= VM_PAGE_ON_SPECULATIVE_Q
) {
499 vm_page_lockspin_queues();
501 if (src_page
->vmp_q_state
!= VM_PAGE_ON_SPECULATIVE_Q
) {
502 vm_page_speculate(src_page
, FALSE
);
504 vm_page_unlock_queues();
508 * Establish pointers to the source
509 * and destination physical pages.
512 upl_phys_page(upl_pl
, (int)(cur_offset
/ PAGE_SIZE
));
513 assert(dst_pnum
!= 0);
515 src_vaddr
= (vm_map_offset_t
)
516 PHYSMAP_PTOV((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(src_page
)
518 dst_vaddr
= (vm_map_offset_t
)
519 PHYSMAP_PTOV((pmap_paddr_t
)dst_pnum
<< PAGE_SHIFT
);
521 #elif __arm__ || __arm64__
522 src_vaddr
= (vm_map_offset_t
)
523 phystokv((pmap_paddr_t
)VM_PAGE_GET_PHYS_PAGE(src_page
)
525 dst_vaddr
= (vm_map_offset_t
)
526 phystokv((pmap_paddr_t
)dst_pnum
<< PAGE_SHIFT
);
528 #error "vm_paging_map_object: no 1-to-1 kernel mapping of physical memory..."
532 src_page_object
= VM_PAGE_OBJECT(src_page
);
535 * Validate the original page...
537 if (src_page_object
->code_signed
) {
538 vm_page_validate_cs_mapped(
540 (const void *) src_vaddr
);
543 * ... and transfer the results to the destination page.
545 UPL_SET_CS_VALIDATED(upl_pl
, cur_offset
/ PAGE_SIZE
,
546 src_page
->vmp_cs_validated
);
547 UPL_SET_CS_TAINTED(upl_pl
, cur_offset
/ PAGE_SIZE
,
548 src_page
->vmp_cs_tainted
);
549 UPL_SET_CS_NX(upl_pl
, cur_offset
/ PAGE_SIZE
,
550 src_page
->vmp_cs_nx
);
553 * page_decrypt() might access a mapped file, so let's release
554 * the object lock for the source page to avoid a potential
555 * deadlock. The source page is kept busy and we have a
556 * "paging_in_progress" reference on its object, so it's safe
557 * to unlock the object here.
559 assert(src_page
->vmp_busy
);
560 assert(src_page_object
->paging_in_progress
> 0);
561 vm_object_unlock(src_page_object
);
564 * Decrypt the encrypted contents of the source page
565 * into the destination page.
567 for (offset_in_page
= 0;
568 offset_in_page
< PAGE_SIZE
;
569 offset_in_page
+= 4096) {
570 if (offset
+ cur_offset
+ offset_in_page
<
571 pager
->crypto_start
||
572 offset
+ cur_offset
+ offset_in_page
>=
574 /* not encrypted: just copy */
575 bcopy((const char *)(src_vaddr
+
577 (char *)(dst_vaddr
+ offset_in_page
),
580 if (apple_protect_pager_data_request_debug
) {
581 printf("apple_protect_data_request"
582 "(%p,0x%llx+0x%llx+0x%04llx): "
583 "out of crypto range "
585 "COPY [0x%016llx 0x%016llx] "
592 (uint64_t) cur_offset
,
593 (uint64_t) offset_in_page
,
596 *(uint64_t *)(dst_vaddr
+
598 *(uint64_t *)(dst_vaddr
+
600 src_page_object
->code_signed
,
601 src_page
->vmp_cs_validated
,
602 src_page
->vmp_cs_tainted
,
603 src_page
->vmp_cs_nx
);
608 ret
= pager
->crypt_info
->page_decrypt(
609 (const void *)(src_vaddr
+ offset_in_page
),
610 (void *)(dst_vaddr
+ offset_in_page
),
611 ((pager
->crypto_backing_offset
-
612 pager
->crypto_start
) + /* XXX ? */
616 pager
->crypt_info
->crypt_ops
);
618 if (apple_protect_pager_data_request_debug
) {
619 printf("apple_protect_data_request"
620 "(%p,0x%llx+0x%llx+0x%04llx): "
621 "in crypto range [0x%llx:0x%llx]: "
622 "DECRYPT offset 0x%llx="
623 "(0x%llx-0x%llx+0x%llx+0x%llx+0x%04llx)"
624 "[0x%016llx 0x%016llx] "
632 (uint64_t) cur_offset
,
633 (uint64_t) offset_in_page
,
634 pager
->crypto_start
, pager
->crypto_end
,
635 ((pager
->crypto_backing_offset
-
636 pager
->crypto_start
) +
640 pager
->crypto_backing_offset
,
643 (uint64_t) cur_offset
,
644 (uint64_t) offset_in_page
,
645 *(uint64_t *)(dst_vaddr
+ offset_in_page
),
646 *(uint64_t *)(dst_vaddr
+ offset_in_page
+ 8),
647 src_page_object
->code_signed
,
648 src_page
->vmp_cs_validated
,
649 src_page
->vmp_cs_tainted
,
659 * Decryption failed. Abort the fault.
661 retval
= KERN_ABORTED
;
664 assert(VM_PAGE_OBJECT(src_page
) == src_page_object
);
665 assert(src_page
->vmp_busy
);
666 assert(src_page_object
->paging_in_progress
> 0);
667 vm_object_lock(src_page_object
);
670 * Cleanup the result of vm_fault_page() of the source page.
672 PAGE_WAKEUP_DONE(src_page
);
673 src_page
= VM_PAGE_NULL
;
674 vm_object_paging_end(src_page_object
);
675 vm_object_unlock(src_page_object
);
677 if (top_page
!= VM_PAGE_NULL
) {
678 assert(VM_PAGE_OBJECT(top_page
) == src_top_object
);
679 vm_object_lock(src_top_object
);
680 VM_PAGE_FREE(top_page
);
681 vm_object_paging_end(src_top_object
);
682 vm_object_unlock(src_top_object
);
688 /* clean up the UPL */
691 * The pages are currently dirty because we've just been
692 * writing on them, but as far as we're concerned, they're
693 * clean since they contain their "original" contents as
694 * provided by us, the pager.
695 * Tell the UPL to mark them "clean".
697 upl_clear_dirty(upl
, TRUE
);
699 /* abort or commit the UPL */
700 if (retval
!= KERN_SUCCESS
) {
702 if (retval
== KERN_ABORTED
) {
703 wait_result_t wait_result
;
706 * We aborted the fault and did not provide
707 * any contents for the requested pages but
708 * the pages themselves are not invalid, so
709 * let's return success and let the caller
710 * retry the fault, in case it might succeed
711 * later (when the decryption code is up and
712 * running in the kernel, for example).
714 retval
= KERN_SUCCESS
;
716 * Wait a little bit first to avoid using
717 * too much CPU time retrying and failing
718 * the same fault over and over again.
720 wait_result
= assert_wait_timeout(
721 (event_t
) apple_protect_pager_data_request
,
725 assert(wait_result
== THREAD_WAITING
);
726 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
727 assert(wait_result
== THREAD_TIMED_OUT
);
731 upl_commit_range(upl
, 0, upl
->size
,
732 UPL_COMMIT_CS_VALIDATED
| UPL_COMMIT_WRITTEN_BY_KERNEL
,
733 upl_pl
, pl_count
, &empty
);
736 /* and deallocate the UPL */
740 if (src_top_object
!= VM_OBJECT_NULL
) {
741 vm_object_deallocate(src_top_object
);
747 * apple_protect_pager_reference()
749 * Get a reference on this memory object.
750 * For external usage only. Assumes that the initial reference count is not 0,
751 * i.e one should not "revive" a dead pager this way.
754 apple_protect_pager_reference(
755 memory_object_t mem_obj
)
757 apple_protect_pager_t pager
;
759 pager
= apple_protect_pager_lookup(mem_obj
);
761 lck_mtx_lock(&apple_protect_pager_lock
);
762 os_ref_retain_locked(&pager
->ref_count
);
763 lck_mtx_unlock(&apple_protect_pager_lock
);
768 * apple_protect_pager_dequeue:
770 * Removes a pager from the list of pagers.
772 * The caller must hold "apple_protect_pager_lock".
775 apple_protect_pager_dequeue(
776 apple_protect_pager_t pager
)
778 assert(!pager
->is_mapped
);
780 queue_remove(&apple_protect_pager_queue
,
782 apple_protect_pager_t
,
784 pager
->pager_queue
.next
= NULL
;
785 pager
->pager_queue
.prev
= NULL
;
787 apple_protect_pager_count
--;
791 * apple_protect_pager_terminate_internal:
793 * Trigger the asynchronous termination of the memory object associated
795 * When the memory object is terminated, there will be one more call
796 * to memory_object_deallocate() (i.e. apple_protect_pager_deallocate())
797 * to finish the clean up.
799 * "apple_protect_pager_lock" should not be held by the caller.
800 * We don't need the lock because the pager has already been removed from
801 * the pagers' list and is now ours exclusively.
804 apple_protect_pager_terminate_internal(
805 apple_protect_pager_t pager
)
807 assert(pager
->is_ready
);
808 assert(!pager
->is_mapped
);
810 if (pager
->backing_object
!= VM_OBJECT_NULL
) {
811 vm_object_deallocate(pager
->backing_object
);
812 pager
->backing_object
= VM_OBJECT_NULL
;
815 /* one less pager using this "pager_crypt_info" */
817 printf("CRYPT_INFO %s: deallocate %p ref %d\n",
820 pager
->crypt_info
->crypt_refcnt
);
821 #endif /* CRYPT_INFO_DEBUG */
822 crypt_info_deallocate(pager
->crypt_info
);
823 pager
->crypt_info
= NULL
;
825 /* trigger the destruction of the memory object */
826 memory_object_destroy(pager
->ap_pgr_hdr
.mo_control
, 0);
830 * apple_protect_pager_deallocate_internal()
832 * Release a reference on this pager and free it when the last
833 * reference goes away.
834 * Can be called with apple_protect_pager_lock held or not but always returns
838 apple_protect_pager_deallocate_internal(
839 apple_protect_pager_t pager
,
842 boolean_t needs_trimming
;
846 lck_mtx_lock(&apple_protect_pager_lock
);
849 count_unmapped
= (apple_protect_pager_count
-
850 apple_protect_pager_count_mapped
);
851 if (count_unmapped
> apple_protect_pager_cache_limit
) {
852 /* we have too many unmapped pagers: trim some */
853 needs_trimming
= TRUE
;
855 needs_trimming
= FALSE
;
858 /* drop a reference on this pager */
859 os_ref_count_t ref_count
= os_ref_release_locked(&pager
->ref_count
);
861 if (ref_count
== 1) {
863 * Only the "named" reference is left, which means that
864 * no one is really holding on to this pager anymore.
867 apple_protect_pager_dequeue(pager
);
868 /* the pager is all ours: no need for the lock now */
869 lck_mtx_unlock(&apple_protect_pager_lock
);
870 apple_protect_pager_terminate_internal(pager
);
871 } else if (ref_count
== 0) {
873 * Dropped the existence reference; the memory object has
874 * been terminated. Do some final cleanup and release the
877 lck_mtx_unlock(&apple_protect_pager_lock
);
878 if (pager
->ap_pgr_hdr
.mo_control
!= MEMORY_OBJECT_CONTROL_NULL
) {
879 memory_object_control_deallocate(pager
->ap_pgr_hdr
.mo_control
);
880 pager
->ap_pgr_hdr
.mo_control
= MEMORY_OBJECT_CONTROL_NULL
;
882 kfree(pager
, sizeof(*pager
));
883 pager
= APPLE_PROTECT_PAGER_NULL
;
885 /* there are still plenty of references: keep going... */
886 lck_mtx_unlock(&apple_protect_pager_lock
);
889 if (needs_trimming
) {
890 apple_protect_pager_trim();
892 /* caution: lock is not held on return... */
896 * apple_protect_pager_deallocate()
898 * Release a reference on this pager and free it when the last
899 * reference goes away.
902 apple_protect_pager_deallocate(
903 memory_object_t mem_obj
)
905 apple_protect_pager_t pager
;
907 PAGER_DEBUG(PAGER_ALL
, ("apple_protect_pager_deallocate: %p\n", mem_obj
));
908 pager
= apple_protect_pager_lookup(mem_obj
);
909 apple_protect_pager_deallocate_internal(pager
, FALSE
);
916 apple_protect_pager_terminate(
920 memory_object_t mem_obj
)
922 PAGER_DEBUG(PAGER_ALL
, ("apple_protect_pager_terminate: %p\n", mem_obj
));
931 apple_protect_pager_synchronize(
932 __unused memory_object_t mem_obj
,
933 __unused memory_object_offset_t offset
,
934 __unused memory_object_size_t length
,
935 __unused vm_sync_t sync_flags
)
937 panic("apple_protect_pager_synchronize: memory_object_synchronize no longer supported\n");
942 * apple_protect_pager_map()
944 * This allows VM to let us, the EMM, know that this memory object
945 * is currently mapped one or more times. This is called by VM each time
946 * the memory object gets mapped and we take one extra reference on the
947 * memory object to account for all its mappings.
950 apple_protect_pager_map(
951 memory_object_t mem_obj
,
952 __unused vm_prot_t prot
)
954 apple_protect_pager_t pager
;
956 PAGER_DEBUG(PAGER_ALL
, ("apple_protect_pager_map: %p\n", mem_obj
));
958 pager
= apple_protect_pager_lookup(mem_obj
);
960 lck_mtx_lock(&apple_protect_pager_lock
);
961 assert(pager
->is_ready
);
962 assert(os_ref_get_count(&pager
->ref_count
) > 0); /* pager is alive */
963 if (pager
->is_mapped
== FALSE
) {
965 * First mapping of this pager: take an extra reference
966 * that will remain until all the mappings of this pager
969 pager
->is_mapped
= TRUE
;
970 os_ref_retain_locked(&pager
->ref_count
);
971 apple_protect_pager_count_mapped
++;
973 lck_mtx_unlock(&apple_protect_pager_lock
);
979 * apple_protect_pager_last_unmap()
981 * This is called by VM when this memory object is no longer mapped anywhere.
984 apple_protect_pager_last_unmap(
985 memory_object_t mem_obj
)
987 apple_protect_pager_t pager
;
990 PAGER_DEBUG(PAGER_ALL
,
991 ("apple_protect_pager_last_unmap: %p\n", mem_obj
));
993 pager
= apple_protect_pager_lookup(mem_obj
);
995 lck_mtx_lock(&apple_protect_pager_lock
);
996 if (pager
->is_mapped
) {
998 * All the mappings are gone, so let go of the one extra
999 * reference that represents all the mappings of this pager.
1001 apple_protect_pager_count_mapped
--;
1002 count_unmapped
= (apple_protect_pager_count
-
1003 apple_protect_pager_count_mapped
);
1004 if (count_unmapped
> apple_protect_pager_count_unmapped_max
) {
1005 apple_protect_pager_count_unmapped_max
= count_unmapped
;
1007 pager
->is_mapped
= FALSE
;
1008 apple_protect_pager_deallocate_internal(pager
, TRUE
);
1009 /* caution: deallocate_internal() released the lock ! */
1011 lck_mtx_unlock(&apple_protect_pager_lock
);
1014 return KERN_SUCCESS
;
1021 apple_protect_pager_t
1022 apple_protect_pager_lookup(
1023 memory_object_t mem_obj
)
1025 apple_protect_pager_t pager
;
1027 assert(mem_obj
->mo_pager_ops
== &apple_protect_pager_ops
);
1028 pager
= (apple_protect_pager_t
)(uintptr_t) mem_obj
;
1029 assert(os_ref_get_count(&pager
->ref_count
) > 0);
1033 apple_protect_pager_t
1034 apple_protect_pager_create(
1035 vm_object_t backing_object
,
1036 vm_object_offset_t backing_offset
,
1037 vm_object_offset_t crypto_backing_offset
,
1038 struct pager_crypt_info
*crypt_info
,
1039 vm_object_offset_t crypto_start
,
1040 vm_object_offset_t crypto_end
)
1042 apple_protect_pager_t pager
, pager2
;
1043 memory_object_control_t control
;
1045 struct pager_crypt_info
*old_crypt_info
;
1047 pager
= (apple_protect_pager_t
) kalloc(sizeof(*pager
));
1048 if (pager
== APPLE_PROTECT_PAGER_NULL
) {
1049 return APPLE_PROTECT_PAGER_NULL
;
1053 * The vm_map call takes both named entry ports and raw memory
1054 * objects in the same parameter. We need to make sure that
1055 * vm_map does not see this object as a named entry port. So,
1056 * we reserve the first word in the object for a fake ip_kotype
1057 * setting - that will tell vm_map to use it as a memory object.
1059 pager
->ap_pgr_hdr
.mo_ikot
= IKOT_MEMORY_OBJECT
;
1060 pager
->ap_pgr_hdr
.mo_pager_ops
= &apple_protect_pager_ops
;
1061 pager
->ap_pgr_hdr
.mo_control
= MEMORY_OBJECT_CONTROL_NULL
;
1063 pager
->is_ready
= FALSE
;/* not ready until it has a "name" */
1064 os_ref_init_count(&pager
->ref_count
, NULL
, 2); /* existence reference (for the cache) and another for the caller */
1065 pager
->is_mapped
= FALSE
;
1066 pager
->backing_object
= backing_object
;
1067 pager
->backing_offset
= backing_offset
;
1068 pager
->crypto_backing_offset
= crypto_backing_offset
;
1069 pager
->crypto_start
= crypto_start
;
1070 pager
->crypto_end
= crypto_end
;
1071 pager
->crypt_info
= crypt_info
; /* allocated by caller */
1073 #if CRYPT_INFO_DEBUG
1074 printf("CRYPT_INFO %s: crypt_info %p [%p,%p,%p,%d]\n",
1077 crypt_info
->page_decrypt
,
1078 crypt_info
->crypt_end
,
1079 crypt_info
->crypt_ops
,
1080 crypt_info
->crypt_refcnt
);
1081 #endif /* CRYPT_INFO_DEBUG */
1083 vm_object_reference(backing_object
);
1085 old_crypt_info
= NULL
;
1087 lck_mtx_lock(&apple_protect_pager_lock
);
1088 /* see if anyone raced us to create a pager for the same object */
1089 queue_iterate(&apple_protect_pager_queue
,
1091 apple_protect_pager_t
,
1093 if ((pager2
->crypt_info
->page_decrypt
!=
1094 crypt_info
->page_decrypt
) ||
1095 (pager2
->crypt_info
->crypt_end
!=
1096 crypt_info
->crypt_end
) ||
1097 (pager2
->crypt_info
->crypt_ops
!=
1098 crypt_info
->crypt_ops
)) {
1099 /* crypt_info contents do not match: next pager */
1103 /* found a match for crypt_info ... */
1104 if (old_crypt_info
) {
1105 /* ... already switched to that crypt_info */
1106 assert(old_crypt_info
== pager2
->crypt_info
);
1107 } else if (pager2
->crypt_info
!= crypt_info
) {
1108 /* ... switch to that pager's crypt_info */
1109 #if CRYPT_INFO_DEBUG
1110 printf("CRYPT_INFO %s: reference %p ref %d "
1114 pager2
->crypt_info
->crypt_refcnt
);
1115 #endif /* CRYPT_INFO_DEBUG */
1116 old_crypt_info
= pager2
->crypt_info
;
1117 crypt_info_reference(old_crypt_info
);
1118 pager
->crypt_info
= old_crypt_info
;
1121 if (pager2
->backing_object
== backing_object
&&
1122 pager2
->backing_offset
== backing_offset
&&
1123 pager2
->crypto_backing_offset
== crypto_backing_offset
&&
1124 pager2
->crypto_start
== crypto_start
&&
1125 pager2
->crypto_end
== crypto_end
) {
1126 /* full match: use that pager */
1130 if (!queue_end(&apple_protect_pager_queue
,
1131 (queue_entry_t
) pager2
)) {
1132 /* we lost the race, down with the loser... */
1133 lck_mtx_unlock(&apple_protect_pager_lock
);
1134 vm_object_deallocate(pager
->backing_object
);
1135 pager
->backing_object
= VM_OBJECT_NULL
;
1136 #if CRYPT_INFO_DEBUG
1137 printf("CRYPT_INFO %s: %p ref %d (create pager match)\n",
1140 pager
->crypt_info
->crypt_refcnt
);
1141 #endif /* CRYPT_INFO_DEBUG */
1142 crypt_info_deallocate(pager
->crypt_info
);
1143 pager
->crypt_info
= NULL
;
1144 kfree(pager
, sizeof(*pager
));
1145 /* ... and go with the winner */
1147 /* let the winner make sure the pager gets ready */
1151 /* enter new pager at the head of our list of pagers */
1152 queue_enter_first(&apple_protect_pager_queue
,
1154 apple_protect_pager_t
,
1156 apple_protect_pager_count
++;
1157 if (apple_protect_pager_count
> apple_protect_pager_count_max
) {
1158 apple_protect_pager_count_max
= apple_protect_pager_count
;
1160 lck_mtx_unlock(&apple_protect_pager_lock
);
1162 kr
= memory_object_create_named((memory_object_t
) pager
,
1165 assert(kr
== KERN_SUCCESS
);
1167 lck_mtx_lock(&apple_protect_pager_lock
);
1168 /* the new pager is now ready to be used */
1169 pager
->is_ready
= TRUE
;
1170 lck_mtx_unlock(&apple_protect_pager_lock
);
1172 /* wakeup anyone waiting for this pager to be ready */
1173 thread_wakeup(&pager
->is_ready
);
1175 if (old_crypt_info
!= NULL
&&
1176 old_crypt_info
!= crypt_info
) {
1177 /* we re-used an old crypt_info instead of using our new one */
1178 #if CRYPT_INFO_DEBUG
1179 printf("CRYPT_INFO %s: deallocate %p ref %d "
1180 "(create used old)\n",
1183 crypt_info
->crypt_refcnt
);
1184 #endif /* CRYPT_INFO_DEBUG */
1185 crypt_info_deallocate(crypt_info
);
1193 * apple_protect_pager_setup()
1195 * Provide the caller with a memory object backed by the provided
1196 * "backing_object" VM object. If such a memory object already exists,
1197 * re-use it, otherwise create a new memory object.
1200 apple_protect_pager_setup(
1201 vm_object_t backing_object
,
1202 vm_object_offset_t backing_offset
,
1203 vm_object_offset_t crypto_backing_offset
,
1204 struct pager_crypt_info
*crypt_info
,
1205 vm_object_offset_t crypto_start
,
1206 vm_object_offset_t crypto_end
)
1208 apple_protect_pager_t pager
;
1209 struct pager_crypt_info
*old_crypt_info
, *new_crypt_info
;
1211 #if CRYPT_INFO_DEBUG
1212 printf("CRYPT_INFO %s: crypt_info=%p [%p,%p,%p,%d]\n",
1215 crypt_info
->page_decrypt
,
1216 crypt_info
->crypt_end
,
1217 crypt_info
->crypt_ops
,
1218 crypt_info
->crypt_refcnt
);
1219 #endif /* CRYPT_INFO_DEBUG */
1221 old_crypt_info
= NULL
;
1223 lck_mtx_lock(&apple_protect_pager_lock
);
1225 queue_iterate(&apple_protect_pager_queue
,
1227 apple_protect_pager_t
,
1229 if ((pager
->crypt_info
->page_decrypt
!=
1230 crypt_info
->page_decrypt
) ||
1231 (pager
->crypt_info
->crypt_end
!=
1232 crypt_info
->crypt_end
) ||
1233 (pager
->crypt_info
->crypt_ops
!=
1234 crypt_info
->crypt_ops
)) {
1235 /* no match for "crypt_info": next pager */
1238 /* found a match for crypt_info ... */
1239 if (old_crypt_info
) {
1240 /* ... already switched to that crypt_info */
1241 assert(old_crypt_info
== pager
->crypt_info
);
1243 /* ... switch to that pager's crypt_info */
1244 old_crypt_info
= pager
->crypt_info
;
1245 #if CRYPT_INFO_DEBUG
1246 printf("CRYPT_INFO %s: "
1247 "switching crypt_info from %p [%p,%p,%p,%d] "
1248 "to %p [%p,%p,%p,%d] from pager %p\n",
1251 crypt_info
->page_decrypt
,
1252 crypt_info
->crypt_end
,
1253 crypt_info
->crypt_ops
,
1254 crypt_info
->crypt_refcnt
,
1256 old_crypt_info
->page_decrypt
,
1257 old_crypt_info
->crypt_end
,
1258 old_crypt_info
->crypt_ops
,
1259 old_crypt_info
->crypt_refcnt
,
1261 printf("CRYPT_INFO %s: %p ref %d (setup match)\n",
1264 pager
->crypt_info
->crypt_refcnt
);
1265 #endif /* CRYPT_INFO_DEBUG */
1266 crypt_info_reference(pager
->crypt_info
);
1269 if (pager
->backing_object
== backing_object
&&
1270 pager
->backing_offset
== backing_offset
&&
1271 pager
->crypto_backing_offset
== crypto_backing_offset
&&
1272 pager
->crypto_start
== crypto_start
&&
1273 pager
->crypto_end
== crypto_end
) {
1274 /* full match: use that pager! */
1275 assert(old_crypt_info
== pager
->crypt_info
);
1276 assert(old_crypt_info
->crypt_refcnt
> 1);
1277 #if CRYPT_INFO_DEBUG
1278 printf("CRYPT_INFO %s: "
1279 "pager match with %p crypt_info %p\n",
1283 printf("CRYPT_INFO %s: deallocate %p ref %d "
1287 old_crypt_info
->crypt_refcnt
);
1288 #endif /* CRYPT_INFO_DEBUG */
1289 /* release the extra ref on crypt_info we got above */
1290 crypt_info_deallocate(old_crypt_info
);
1291 assert(old_crypt_info
->crypt_refcnt
> 0);
1292 /* give extra reference on pager to the caller */
1293 os_ref_retain_locked(&pager
->ref_count
);
1297 if (queue_end(&apple_protect_pager_queue
,
1298 (queue_entry_t
) pager
)) {
1299 lck_mtx_unlock(&apple_protect_pager_lock
);
1300 /* no existing pager for this backing object */
1301 pager
= APPLE_PROTECT_PAGER_NULL
;
1302 if (old_crypt_info
) {
1303 /* use this old crypt_info for new pager */
1304 new_crypt_info
= old_crypt_info
;
1305 #if CRYPT_INFO_DEBUG
1306 printf("CRYPT_INFO %s: "
1307 "will use old_crypt_info %p for new pager\n",
1310 #endif /* CRYPT_INFO_DEBUG */
1312 /* allocate a new crypt_info for new pager */
1313 new_crypt_info
= kalloc(sizeof(*new_crypt_info
));
1314 *new_crypt_info
= *crypt_info
;
1315 new_crypt_info
->crypt_refcnt
= 1;
1316 #if CRYPT_INFO_DEBUG
1317 printf("CRYPT_INFO %s: "
1318 "will use new_crypt_info %p for new pager\n",
1321 #endif /* CRYPT_INFO_DEBUG */
1323 if (new_crypt_info
== NULL
) {
1324 /* can't create new pager without a crypt_info */
1326 /* create new pager */
1327 pager
= apple_protect_pager_create(
1330 crypto_backing_offset
,
1335 if (pager
== APPLE_PROTECT_PAGER_NULL
) {
1336 /* could not create a new pager */
1337 if (new_crypt_info
== old_crypt_info
) {
1338 /* release extra reference on old_crypt_info */
1339 #if CRYPT_INFO_DEBUG
1340 printf("CRYPT_INFO %s: deallocate %p ref %d "
1341 "(create fail old_crypt_info)\n",
1344 old_crypt_info
->crypt_refcnt
);
1345 #endif /* CRYPT_INFO_DEBUG */
1346 crypt_info_deallocate(old_crypt_info
);
1347 old_crypt_info
= NULL
;
1349 /* release unused new_crypt_info */
1350 assert(new_crypt_info
->crypt_refcnt
== 1);
1351 #if CRYPT_INFO_DEBUG
1352 printf("CRYPT_INFO %s: deallocate %p ref %d "
1353 "(create fail new_crypt_info)\n",
1356 new_crypt_info
->crypt_refcnt
);
1357 #endif /* CRYPT_INFO_DEBUG */
1358 crypt_info_deallocate(new_crypt_info
);
1359 new_crypt_info
= NULL
;
1361 return MEMORY_OBJECT_NULL
;
1363 lck_mtx_lock(&apple_protect_pager_lock
);
1365 assert(old_crypt_info
== pager
->crypt_info
);
1368 while (!pager
->is_ready
) {
1369 lck_mtx_sleep(&apple_protect_pager_lock
,
1374 lck_mtx_unlock(&apple_protect_pager_lock
);
1376 return (memory_object_t
) pager
;
1380 apple_protect_pager_trim(void)
1382 apple_protect_pager_t pager
, prev_pager
;
1383 queue_head_t trim_queue
;
1387 lck_mtx_lock(&apple_protect_pager_lock
);
1390 * We have too many pagers, try and trim some unused ones,
1391 * starting with the oldest pager at the end of the queue.
1393 queue_init(&trim_queue
);
1396 for (pager
= (apple_protect_pager_t
)
1397 queue_last(&apple_protect_pager_queue
);
1398 !queue_end(&apple_protect_pager_queue
,
1399 (queue_entry_t
) pager
);
1400 pager
= prev_pager
) {
1401 /* get prev elt before we dequeue */
1402 prev_pager
= (apple_protect_pager_t
)
1403 queue_prev(&pager
->pager_queue
);
1405 if (os_ref_get_count(&pager
->ref_count
) == 2 &&
1407 !pager
->is_mapped
) {
1408 /* this pager can be trimmed */
1410 /* remove this pager from the main list ... */
1411 apple_protect_pager_dequeue(pager
);
1412 /* ... and add it to our trim queue */
1413 queue_enter_first(&trim_queue
,
1415 apple_protect_pager_t
,
1418 count_unmapped
= (apple_protect_pager_count
-
1419 apple_protect_pager_count_mapped
);
1420 if (count_unmapped
<= apple_protect_pager_cache_limit
) {
1421 /* we have enough pagers to trim */
1426 if (num_trim
> apple_protect_pager_num_trim_max
) {
1427 apple_protect_pager_num_trim_max
= num_trim
;
1429 apple_protect_pager_num_trim_total
+= num_trim
;
1431 lck_mtx_unlock(&apple_protect_pager_lock
);
1433 /* terminate the trimmed pagers */
1434 while (!queue_empty(&trim_queue
)) {
1435 queue_remove_first(&trim_queue
,
1437 apple_protect_pager_t
,
1439 pager
->pager_queue
.next
= NULL
;
1440 pager
->pager_queue
.prev
= NULL
;
1442 * We can't call deallocate_internal() because the pager
1443 * has already been dequeued, but we still need to remove
1446 os_ref_count_t __assert_only count
= os_ref_release_locked(&pager
->ref_count
);
1448 apple_protect_pager_terminate_internal(pager
);
1454 crypt_info_reference(
1455 struct pager_crypt_info
*crypt_info
)
1457 assert(crypt_info
->crypt_refcnt
!= 0);
1458 #if CRYPT_INFO_DEBUG
1459 printf("CRYPT_INFO %s: %p ref %d -> %d\n",
1462 crypt_info
->crypt_refcnt
,
1463 crypt_info
->crypt_refcnt
+ 1);
1464 #endif /* CRYPT_INFO_DEBUG */
1465 OSAddAtomic(+1, &crypt_info
->crypt_refcnt
);
1469 crypt_info_deallocate(
1470 struct pager_crypt_info
*crypt_info
)
1472 #if CRYPT_INFO_DEBUG
1473 printf("CRYPT_INFO %s: %p ref %d -> %d\n",
1476 crypt_info
->crypt_refcnt
,
1477 crypt_info
->crypt_refcnt
- 1);
1478 #endif /* CRYPT_INFO_DEBUG */
1479 OSAddAtomic(-1, &crypt_info
->crypt_refcnt
);
1480 if (crypt_info
->crypt_refcnt
== 0) {
1481 /* deallocate any crypt module data */
1482 if (crypt_info
->crypt_end
) {
1483 crypt_info
->crypt_end(crypt_info
->crypt_ops
);
1484 crypt_info
->crypt_end
= NULL
;
1486 #if CRYPT_INFO_DEBUG
1487 printf("CRYPT_INFO %s: freeing %p\n",
1490 #endif /* CRYPT_INFO_DEBUG */
1491 kfree(crypt_info
, sizeof(*crypt_info
));