forked from apple/darwin-xnu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkdp_core.c
1520 lines (1292 loc) · 46.2 KB
/
kdp_core.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2015-2017 Apple Computer, Inc. All rights reserved.
*
* @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
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
#ifdef CONFIG_KDP_INTERACTIVE_DEBUGGING
#include <mach/mach_types.h>
#include <mach/vm_attributes.h>
#include <mach/vm_param.h>
#include <mach/vm_map.h>
#include <vm/vm_protos.h>
#include <vm/vm_kern.h>
#include <vm/vm_map.h>
#include <machine/cpu_capabilities.h>
#include <libsa/types.h>
#include <libkern/kernel_mach_header.h>
#include <libkern/zlib.h>
#include <kdp/kdp_internal.h>
#include <kdp/kdp_core.h>
#include <kdp/processor_core.h>
#include <IOKit/IOPolledInterface.h>
#include <IOKit/IOBSD.h>
#include <sys/errno.h>
#include <sys/msgbuf.h>
#include <san/kasan.h>
#if defined(__x86_64__)
#include <i386/pmap_internal.h>
#include <kdp/ml/i386/kdp_x86_common.h>
#include <kern/debug.h>
#endif /* defined(__x86_64__) */
#if CONFIG_EMBEDDED
#include <arm/cpuid.h>
#include <arm/caches_internal.h>
#include <pexpert/arm/consistent_debug.h>
#if !defined(ROUNDUP)
#define ROUNDUP(a, b) (((a) + ((b) - 1)) & (~((b) - 1)))
#endif
#if !defined(ROUNDDOWN)
#define ROUNDDOWN(a, b) ((a) & ~((b) - 1))
#endif
#endif /* CONFIG_EMBEDDED */
typedef int (*pmap_traverse_callback)(vm_map_offset_t start,
vm_map_offset_t end,
void *context);
extern int pmap_traverse_present_mappings(pmap_t pmap,
vm_map_offset_t start,
vm_map_offset_t end,
pmap_traverse_callback callback,
void *context);
static int kern_dump_save_summary(void *refcon, core_save_summary_cb callback, void *context);
static int kern_dump_save_seg_descriptions(void *refcon, core_save_segment_descriptions_cb callback, void *context);
static int kern_dump_save_thread_state(void *refcon, void *buf, core_save_thread_state_cb callback, void *context);
static int kern_dump_save_sw_vers(void *refcon, core_save_sw_vers_cb callback, void *context);
static int kern_dump_save_segment_data(void *refcon, core_save_segment_data_cb callback, void *context);
static int
kern_dump_pmap_traverse_preflight_callback(vm_map_offset_t start,
vm_map_offset_t end,
void *context);
static int
kern_dump_pmap_traverse_send_segdesc_callback(vm_map_offset_t start,
vm_map_offset_t end,
void *context);
static int
kern_dump_pmap_traverse_send_segdata_callback(vm_map_offset_t start,
vm_map_offset_t end,
void *context);
struct kdp_core_out_vars;
typedef int (*kern_dump_output_proc)(unsigned int request, char *corename,
uint64_t length, void *panic_data);
struct kdp_core_out_vars
{
kern_dump_output_proc outproc;
z_output_func zoutput;
size_t zipped;
uint64_t totalbytes;
uint64_t lastpercent;
IOReturn error;
unsigned outremain;
unsigned outlen;
unsigned writes;
Bytef * outbuf;
};
extern uint32_t kdp_crashdump_pkt_size;
static vm_offset_t kdp_core_zmem;
static size_t kdp_core_zsize;
static size_t kdp_core_zoffset;
static z_stream kdp_core_zs;
static uint64_t kdp_core_total_size;
static uint64_t kdp_core_total_size_sent_uncomp;
#if CONFIG_EMBEDDED
struct xnu_hw_shmem_dbg_command_info *hwsd_info = NULL;
#define KDP_CORE_HW_SHMEM_DBG_NUM_BUFFERS 2
#define KDP_CORE_HW_SHMEM_DBG_TOTAL_BUF_SIZE 64 * 1024
/*
* Astris can read up to 4064 bytes at a time over
* the probe, so we should try to make our buffer
* size a multiple of this to make reads by astris
* (the bottleneck) most efficient.
*/
#define OPTIMAL_ASTRIS_READSIZE 4064
struct kdp_hw_shmem_dbg_buf_elm {
vm_offset_t khsd_buf;
uint32_t khsd_data_length;
STAILQ_ENTRY(kdp_hw_shmem_dbg_buf_elm) khsd_elms;
};
static STAILQ_HEAD(, kdp_hw_shmem_dbg_buf_elm) free_hw_shmem_dbg_bufs =
STAILQ_HEAD_INITIALIZER(free_hw_shmem_dbg_bufs);
static STAILQ_HEAD(, kdp_hw_shmem_dbg_buf_elm) hw_shmem_dbg_bufs_to_flush =
STAILQ_HEAD_INITIALIZER(hw_shmem_dbg_bufs_to_flush);
static struct kdp_hw_shmem_dbg_buf_elm *currently_filling_buf = NULL;
static struct kdp_hw_shmem_dbg_buf_elm *currently_flushing_buf = NULL;
static uint32_t kdp_hw_shmem_dbg_bufsize = 0;
static uint32_t kdp_hw_shmem_dbg_seq_no = 0;
static uint64_t kdp_hw_shmem_dbg_contact_deadline = 0;
static uint64_t kdp_hw_shmem_dbg_contact_deadline_interval = 0;
#define KDP_HW_SHMEM_DBG_TIMEOUT_DEADLINE_SECS 30
#endif /* CONFIG_EMBEDDED */
static boolean_t kern_dump_successful = FALSE;
struct mach_core_fileheader kdp_core_header = { };
/*
* These variables will be modified by the BSD layer if the root device is
* a RAMDisk.
*/
uint64_t kdp_core_ramdisk_addr = 0;
uint64_t kdp_core_ramdisk_size = 0;
boolean_t kdp_has_polled_corefile(void)
{
return (NULL != gIOPolledCoreFileVars);
}
#if CONFIG_EMBEDDED
/*
* Whenever we start a coredump, make sure the buffers
* are all on the free queue and the state is as expected.
* The buffers may have been left in a different state if
* a previous coredump attempt failed.
*/
static void
kern_dump_hw_shmem_dbg_reset()
{
struct kdp_hw_shmem_dbg_buf_elm *cur_elm = NULL, *tmp_elm = NULL;
STAILQ_FOREACH(cur_elm, &free_hw_shmem_dbg_bufs, khsd_elms) {
cur_elm->khsd_data_length = 0;
}
if (currently_filling_buf != NULL) {
currently_filling_buf->khsd_data_length = 0;
STAILQ_INSERT_HEAD(&free_hw_shmem_dbg_bufs, currently_filling_buf, khsd_elms);
currently_filling_buf = NULL;
}
if (currently_flushing_buf != NULL) {
currently_flushing_buf->khsd_data_length = 0;
STAILQ_INSERT_HEAD(&free_hw_shmem_dbg_bufs, currently_flushing_buf, khsd_elms);
currently_flushing_buf = NULL;
}
STAILQ_FOREACH_SAFE(cur_elm, &hw_shmem_dbg_bufs_to_flush, khsd_elms, tmp_elm) {
cur_elm->khsd_data_length = 0;
STAILQ_REMOVE(&hw_shmem_dbg_bufs_to_flush, cur_elm, kdp_hw_shmem_dbg_buf_elm, khsd_elms);
STAILQ_INSERT_HEAD(&free_hw_shmem_dbg_bufs, cur_elm, khsd_elms);
}
hwsd_info->xhsdci_status = XHSDCI_COREDUMP_BUF_EMPTY;
kdp_hw_shmem_dbg_seq_no = 0;
hwsd_info->xhsdci_buf_phys_addr = 0;
hwsd_info->xhsdci_buf_data_length = 0;
hwsd_info->xhsdci_coredump_total_size_uncomp = 0;
hwsd_info->xhsdci_coredump_total_size_sent_uncomp = 0;
hwsd_info->xhsdci_page_size = PAGE_SIZE;
FlushPoC_DcacheRegion((vm_offset_t) hwsd_info, sizeof(*hwsd_info));
kdp_hw_shmem_dbg_contact_deadline = mach_absolute_time() + kdp_hw_shmem_dbg_contact_deadline_interval;
}
/*
* Tries to move buffers forward in 'progress'. If
* the hardware debugger is done consuming the current buffer, we
* can put the next one on it and move the current
* buffer back to the free queue.
*/
static int
kern_dump_hw_shmem_dbg_process_buffers()
{
FlushPoC_DcacheRegion((vm_offset_t) hwsd_info, sizeof(*hwsd_info));
if (hwsd_info->xhsdci_status == XHSDCI_COREDUMP_ERROR) {
kern_coredump_log(NULL, "Detected remote error, terminating...\n");
return -1;
} else if (hwsd_info->xhsdci_status == XHSDCI_COREDUMP_BUF_EMPTY) {
if (hwsd_info->xhsdci_seq_no != (kdp_hw_shmem_dbg_seq_no + 1)) {
kern_coredump_log(NULL, "Detected stale/invalid seq num. Expected: %d, received %d\n",
(kdp_hw_shmem_dbg_seq_no + 1), hwsd_info->xhsdci_seq_no);
hwsd_info->xhsdci_status = XHSDCI_COREDUMP_ERROR;
FlushPoC_DcacheRegion((vm_offset_t) hwsd_info, sizeof(*hwsd_info));
return -1;
}
kdp_hw_shmem_dbg_seq_no = hwsd_info->xhsdci_seq_no;
if (currently_flushing_buf != NULL) {
currently_flushing_buf->khsd_data_length = 0;
STAILQ_INSERT_TAIL(&free_hw_shmem_dbg_bufs, currently_flushing_buf, khsd_elms);
}
currently_flushing_buf = STAILQ_FIRST(&hw_shmem_dbg_bufs_to_flush);
if (currently_flushing_buf != NULL) {
STAILQ_REMOVE_HEAD(&hw_shmem_dbg_bufs_to_flush, khsd_elms);
FlushPoC_DcacheRegion((vm_offset_t) hwsd_info, sizeof(*hwsd_info));
hwsd_info->xhsdci_buf_phys_addr = kvtophys(currently_flushing_buf->khsd_buf);
hwsd_info->xhsdci_buf_data_length = currently_flushing_buf->khsd_data_length;
hwsd_info->xhsdci_coredump_total_size_uncomp = kdp_core_total_size;
hwsd_info->xhsdci_coredump_total_size_sent_uncomp = kdp_core_total_size_sent_uncomp;
FlushPoC_DcacheRegion((vm_offset_t) hwsd_info, KDP_CORE_HW_SHMEM_DBG_TOTAL_BUF_SIZE);
hwsd_info->xhsdci_seq_no = ++kdp_hw_shmem_dbg_seq_no;
hwsd_info->xhsdci_status = XHSDCI_COREDUMP_BUF_READY;
FlushPoC_DcacheRegion((vm_offset_t) hwsd_info, sizeof(*hwsd_info));
}
kdp_hw_shmem_dbg_contact_deadline = mach_absolute_time() +
kdp_hw_shmem_dbg_contact_deadline_interval;
return 0;
} else if (mach_absolute_time() > kdp_hw_shmem_dbg_contact_deadline) {
kern_coredump_log(NULL, "Kernel timed out waiting for hardware debugger to update handshake structure.");
kern_coredump_log(NULL, "No contact in %d seconds\n", KDP_HW_SHMEM_DBG_TIMEOUT_DEADLINE_SECS);
hwsd_info->xhsdci_status = XHSDCI_COREDUMP_ERROR;
FlushPoC_DcacheRegion((vm_offset_t) hwsd_info, sizeof(*hwsd_info));
return -1;
}
return 0;
}
/*
* Populates currently_filling_buf with a new buffer
* once one becomes available. Returns 0 on success
* or the value returned by kern_dump_hw_shmem_dbg_process_buffers()
* if it is non-zero (an error).
*/
static int
kern_dump_hw_shmem_dbg_get_buffer()
{
int ret = 0;
assert(currently_filling_buf == NULL);
while (STAILQ_EMPTY(&free_hw_shmem_dbg_bufs)) {
ret = kern_dump_hw_shmem_dbg_process_buffers();
if (ret) {
return ret;
}
}
currently_filling_buf = STAILQ_FIRST(&free_hw_shmem_dbg_bufs);
STAILQ_REMOVE_HEAD(&free_hw_shmem_dbg_bufs, khsd_elms);
assert(currently_filling_buf->khsd_data_length == 0);
return ret;
}
/*
* Output procedure for hardware shared memory core dumps
*
* Tries to fill up the buffer completely before flushing
*/
static int
kern_dump_hw_shmem_dbg_buffer_proc(unsigned int request, __unused char *corename,
uint64_t length, void * data)
{
int ret = 0;
assert(length < UINT32_MAX);
uint32_t bytes_remaining = (uint32_t) length;
uint32_t bytes_to_copy;
if (request == KDP_EOF) {
assert(currently_filling_buf == NULL);
/*
* Wait until we've flushed all the buffers
* before setting the connection status to done.
*/
while (!STAILQ_EMPTY(&hw_shmem_dbg_bufs_to_flush) ||
currently_flushing_buf != NULL) {
ret = kern_dump_hw_shmem_dbg_process_buffers();
if (ret) {
return ret;
}
}
/*
* If the last status we saw indicates that the buffer was
* empty and we didn't flush any new data since then, we expect
* the sequence number to still match the last we saw.
*/
if (hwsd_info->xhsdci_seq_no < kdp_hw_shmem_dbg_seq_no) {
kern_coredump_log(NULL, "EOF Flush: Detected stale/invalid seq num. Expected: %d, received %d\n",
kdp_hw_shmem_dbg_seq_no, hwsd_info->xhsdci_seq_no);
return -1;
}
kdp_hw_shmem_dbg_seq_no = hwsd_info->xhsdci_seq_no;
kern_coredump_log(NULL, "Setting coredump status as done!\n");
hwsd_info->xhsdci_seq_no = ++kdp_hw_shmem_dbg_seq_no;
hwsd_info->xhsdci_status = XHSDCI_COREDUMP_STATUS_DONE;
FlushPoC_DcacheRegion((vm_offset_t) hwsd_info, sizeof(*hwsd_info));
return ret;
}
assert(request == KDP_DATA);
/*
* The output procedure is called with length == 0 and data == NULL
* to flush any remaining output at the end of the coredump before
* we call it a final time to mark the dump as done.
*/
if (length == 0) {
assert(data == NULL);
if (currently_filling_buf != NULL) {
STAILQ_INSERT_TAIL(&hw_shmem_dbg_bufs_to_flush, currently_filling_buf, khsd_elms);
currently_filling_buf = NULL;
}
/*
* Move the current buffer along if possible.
*/
ret = kern_dump_hw_shmem_dbg_process_buffers();
return ret;
}
while (bytes_remaining != 0) {
/*
* Make sure we have a buffer to work with.
*/
while (currently_filling_buf == NULL) {
ret = kern_dump_hw_shmem_dbg_get_buffer();
if (ret) {
return ret;
}
}
assert(kdp_hw_shmem_dbg_bufsize >= currently_filling_buf->khsd_data_length);
bytes_to_copy = MIN(bytes_remaining, kdp_hw_shmem_dbg_bufsize -
currently_filling_buf->khsd_data_length);
bcopy(data, (void *)(currently_filling_buf->khsd_buf + currently_filling_buf->khsd_data_length),
bytes_to_copy);
currently_filling_buf->khsd_data_length += bytes_to_copy;
if (currently_filling_buf->khsd_data_length == kdp_hw_shmem_dbg_bufsize) {
STAILQ_INSERT_TAIL(&hw_shmem_dbg_bufs_to_flush, currently_filling_buf, khsd_elms);
currently_filling_buf = NULL;
/*
* Move it along if possible.
*/
ret = kern_dump_hw_shmem_dbg_process_buffers();
if (ret) {
return ret;
}
}
bytes_remaining -= bytes_to_copy;
data = (void *) ((uintptr_t)data + bytes_to_copy);
}
return ret;
}
#endif /* CONFIG_EMBEDDED */
static IOReturn
kern_dump_disk_proc(unsigned int request, __unused char *corename,
uint64_t length, void * data)
{
uint64_t noffset;
uint32_t err = kIOReturnSuccess;
switch (request)
{
case KDP_WRQ:
err = IOPolledFileSeek(gIOPolledCoreFileVars, 0);
if (kIOReturnSuccess != err) {
kern_coredump_log(NULL, "IOPolledFileSeek(gIOPolledCoreFileVars, 0) returned 0x%x\n", err);
break;
}
err = IOPolledFilePollersOpen(gIOPolledCoreFileVars, kIOPolledBeforeSleepState, false);
break;
case KDP_SEEK:
noffset = *((uint64_t *) data);
err = IOPolledFileWrite(gIOPolledCoreFileVars, 0, 0, NULL);
if (kIOReturnSuccess != err) {
kern_coredump_log(NULL, "IOPolledFileWrite (during seek) returned 0x%x\n", err);
break;
}
err = IOPolledFileSeek(gIOPolledCoreFileVars, noffset);
if (kIOReturnSuccess != err) {
kern_coredump_log(NULL, "IOPolledFileSeek(0x%llx) returned 0x%x\n", noffset, err);
}
break;
case KDP_DATA:
err = IOPolledFileWrite(gIOPolledCoreFileVars, data, length, NULL);
if (kIOReturnSuccess != err) {
kern_coredump_log(NULL, "IOPolledFileWrite(gIOPolledCoreFileVars, 0x%p, 0x%llx, NULL) returned 0x%x\n",
data, length, err);
break;
}
break;
#if CONFIG_EMBEDDED
/* Only supported on embedded by the underlying polled mode driver */
case KDP_FLUSH:
err = IOPolledFileFlush(gIOPolledCoreFileVars);
if (kIOReturnSuccess != err) {
kern_coredump_log(NULL, "IOPolledFileFlush() returned 0x%x\n", err);
break;
}
break;
#endif
case KDP_EOF:
err = IOPolledFileWrite(gIOPolledCoreFileVars, 0, 0, NULL);
if (kIOReturnSuccess != err) {
kern_coredump_log(NULL, "IOPolledFileWrite (during EOF) returned 0x%x\n", err);
break;
}
err = IOPolledFilePollersClose(gIOPolledCoreFileVars, kIOPolledBeforeSleepState);
if (kIOReturnSuccess != err) {
kern_coredump_log(NULL, "IOPolledFilePollersClose (during EOF) returned 0x%x\n", err);
break;
}
break;
}
return (err);
}
/*
* flushes any data to the output proc immediately
*/
static int
kdp_core_zoutput(z_streamp strm, Bytef *buf, unsigned len)
{
struct kdp_core_out_vars * vars = (typeof(vars)) strm->opaque;
IOReturn ret;
vars->zipped += len;
if (vars->error >= 0)
{
if ((ret = (*vars->outproc)(KDP_DATA, NULL, len, buf)) != kIOReturnSuccess)
{
kern_coredump_log(NULL, "(kdp_core_zoutput) outproc(KDP_DATA, NULL, 0x%x, 0x%p) returned 0x%x\n",
len, buf, ret);
vars->error = ret;
}
if (!buf && !len) kern_coredump_log(NULL, "100..");
}
return (len);
}
/*
* tries to fill the buffer with data before flushing it via the output proc.
*/
static int
kdp_core_zoutputbuf(z_streamp strm, Bytef *inbuf, unsigned inlen)
{
struct kdp_core_out_vars * vars = (typeof(vars)) strm->opaque;
unsigned remain;
IOReturn ret;
unsigned chunk;
boolean_t flush;
remain = inlen;
vars->zipped += inlen;
flush = (!inbuf && !inlen);
while ((vars->error >= 0) && (remain || flush))
{
chunk = vars->outremain;
if (chunk > remain) chunk = remain;
if (!inbuf) bzero(&vars->outbuf[vars->outlen - vars->outremain], chunk);
else
{
bcopy(inbuf, &vars->outbuf[vars->outlen - vars->outremain], chunk);
inbuf += chunk;
}
vars->outremain -= chunk;
remain -= chunk;
if (vars->outremain && !flush) break;
if ((ret = (*vars->outproc)(KDP_DATA, NULL,
vars->outlen - vars->outremain,
vars->outbuf)) != kIOReturnSuccess)
{
kern_coredump_log(NULL, "(kdp_core_zoutputbuf) outproc(KDP_DATA, NULL, 0x%x, 0x%p) returned 0x%x\n",
(vars->outlen - vars->outremain), vars->outbuf, ret);
vars->error = ret;
}
if (flush)
{
kern_coredump_log(NULL, "100..");
flush = false;
}
vars->outremain = vars->outlen;
}
return (inlen);
}
static int
kdp_core_zinput(z_streamp strm, Bytef *buf, unsigned size)
{
struct kdp_core_out_vars * vars = (typeof(vars)) strm->opaque;
uint64_t percent, total_in = 0;
unsigned len;
len = strm->avail_in;
if (len > size) len = size;
if (len == 0) return 0;
if (strm->next_in != (Bytef *) strm) memcpy(buf, strm->next_in, len);
else bzero(buf, len);
strm->adler = z_crc32(strm->adler, buf, len);
strm->avail_in -= len;
strm->next_in += len;
strm->total_in += len;
if (0 == (511 & vars->writes++))
{
total_in = strm->total_in;
kdp_core_total_size_sent_uncomp = strm->total_in;
percent = (total_in * 100) / vars->totalbytes;
if ((percent - vars->lastpercent) >= 10)
{
vars->lastpercent = percent;
kern_coredump_log(NULL, "%lld..\n", percent);
}
}
return (int)len;
}
static IOReturn
kdp_core_stream_output_chunk(struct kdp_core_out_vars * vars, unsigned length, void * data)
{
z_stream * zs;
int zr;
boolean_t flush;
zs = &kdp_core_zs;
if (kdp_corezip_disabled)
{
(*vars->zoutput)(zs, data, length);
}
else
{
flush = (!length && !data);
zr = Z_OK;
assert(!zs->avail_in);
while (vars->error >= 0)
{
if (!zs->avail_in && !flush)
{
if (!length) break;
zs->next_in = data ? data : (Bytef *) zs /* zero marker */;
zs->avail_in = length;
length = 0;
}
if (!zs->avail_out)
{
zs->next_out = (Bytef *) zs;
zs->avail_out = UINT32_MAX;
}
zr = deflate(zs, flush ? Z_FINISH : Z_NO_FLUSH);
if (Z_STREAM_END == zr) break;
if (zr != Z_OK)
{
kern_coredump_log(NULL, "ZERR %d\n", zr);
vars->error = zr;
}
}
if (flush) (*vars->zoutput)(zs, NULL, 0);
}
return (vars->error);
}
kern_return_t
kdp_core_output(void *kdp_core_out_vars, uint64_t length, void * data)
{
IOReturn err;
unsigned int chunk;
enum { kMaxZLibChunk = 1024*1024*1024 };
struct kdp_core_out_vars *vars = (struct kdp_core_out_vars *)kdp_core_out_vars;
do
{
if (length <= kMaxZLibChunk) chunk = (typeof(chunk)) length;
else chunk = kMaxZLibChunk;
err = kdp_core_stream_output_chunk(vars, chunk, data);
length -= chunk;
if (data) data = (void *) (((uintptr_t) data) + chunk);
}
while (length && (kIOReturnSuccess == err));
return (err);
}
#if defined(__arm__) || defined(__arm64__)
extern pmap_paddr_t avail_start, avail_end;
extern struct vm_object pmap_object_store;
#endif
extern vm_offset_t c_buffers;
extern vm_size_t c_buffers_size;
ppnum_t
kernel_pmap_present_mapping(uint64_t vaddr, uint64_t * pvincr, uintptr_t * pvphysaddr)
{
ppnum_t ppn = 0;
uint64_t vincr = PAGE_SIZE_64;
assert(!(vaddr & PAGE_MASK_64));
/* VA ranges to exclude */
if (vaddr == c_buffers)
{
/* compressor data */
ppn = 0;
vincr = c_buffers_size;
}
else if (vaddr == kdp_core_zmem)
{
/* zlib working memory */
ppn = 0;
vincr = kdp_core_zsize;
}
else if ((kdp_core_ramdisk_addr != 0) && (vaddr == kdp_core_ramdisk_addr))
{
ppn = 0;
vincr = kdp_core_ramdisk_size;
}
else
#if defined(__arm64__)
if (vaddr == _COMM_PAGE64_BASE_ADDRESS)
{
/* not readable */
ppn = 0;
vincr = _COMM_PAGE_AREA_LENGTH;
}
else
#endif /* defined(__arm64__) */
#if defined(__arm__) || defined(__arm64__)
if (vaddr == phystokv(avail_start))
{
/* physical memory map */
ppn = 0;
vincr = (avail_end - avail_start);
}
else
#endif /* defined(__arm__) || defined(__arm64__) */
ppn = pmap_find_phys(kernel_pmap, vaddr);
*pvincr = round_page_64(vincr);
if (ppn && pvphysaddr)
{
uint64_t phys = ptoa_64(ppn);
#if defined(__arm__) || defined(__arm64__)
if (isphysmem(phys)) *pvphysaddr = phystokv(phys);
#else
if (physmap_enclosed(phys)) *pvphysaddr = (uintptr_t)PHYSMAP_PTOV(phys);
#endif
else ppn = 0;
}
return (ppn);
}
int
pmap_traverse_present_mappings(pmap_t __unused pmap,
vm_map_offset_t start,
vm_map_offset_t end,
pmap_traverse_callback callback,
void *context)
{
IOReturn ret;
vm_map_offset_t vcurstart, vcur;
uint64_t vincr = 0;
vm_map_offset_t debug_start;
vm_map_offset_t debug_end;
boolean_t lastvavalid;
#if defined(__arm__) || defined(__arm64__)
vm_page_t m = VM_PAGE_NULL;
#endif
debug_start = trunc_page((vm_map_offset_t) debug_buf_base);
debug_end = round_page((vm_map_offset_t) (debug_buf_base + debug_buf_size));
#if defined(__x86_64__)
assert(!is_ept_pmap(pmap));
#endif
/* Assumes pmap is locked, or being called from the kernel debugger */
if (start > end) return (KERN_INVALID_ARGUMENT);
ret = KERN_SUCCESS;
lastvavalid = FALSE;
for (vcur = vcurstart = start; (ret == KERN_SUCCESS) && (vcur < end); ) {
ppnum_t ppn = 0;
#if defined(__arm__) || defined(__arm64__)
/* We're at the start of the physmap, so pull out the pagetable pages that
* are accessed through that region.*/
if (vcur == phystokv(avail_start) && vm_object_lock_try_shared(&pmap_object_store))
m = (vm_page_t)vm_page_queue_first(&pmap_object_store.memq);
if (m != VM_PAGE_NULL)
{
vm_map_offset_t vprev = vcur;
ppn = (ppnum_t)atop(avail_end);
while (!vm_page_queue_end(&pmap_object_store.memq, (vm_page_queue_entry_t)m))
{
/* Ignore pages that come from the static region and have already been dumped.*/
if (VM_PAGE_GET_PHYS_PAGE(m) >= atop(avail_start))
{
ppn = VM_PAGE_GET_PHYS_PAGE(m);
break;
}
m = (vm_page_t)vm_page_queue_next(&m->listq);
}
vcur = phystokv(ptoa(ppn));
if (vcur != vprev)
{
ret = callback(vcurstart, vprev, context);
lastvavalid = FALSE;
}
vincr = PAGE_SIZE_64;
if (ppn == atop(avail_end))
{
vm_object_unlock(&pmap_object_store);
m = VM_PAGE_NULL;
}
else
m = (vm_page_t)vm_page_queue_next(&m->listq);
}
if (m == VM_PAGE_NULL)
ppn = kernel_pmap_present_mapping(vcur, &vincr, NULL);
#else /* defined(__arm__) || defined(__arm64__) */
ppn = kernel_pmap_present_mapping(vcur, &vincr, NULL);
#endif
if (ppn != 0)
{
if (((vcur < debug_start) || (vcur >= debug_end))
&& !(EFI_VALID_PAGE(ppn) ||
pmap_valid_page(ppn)))
{
/* not something we want */
ppn = 0;
}
}
if (ppn != 0) {
if (!lastvavalid) {
/* Start of a new virtual region */
vcurstart = vcur;
lastvavalid = TRUE;
}
} else {
if (lastvavalid) {
/* end of a virtual region */
ret = callback(vcurstart, vcur, context);
lastvavalid = FALSE;
}
#if defined(__x86_64__)
/* Try to skip by 2MB if possible */
if (((vcur & PDMASK) == 0) && cpu_64bit) {
pd_entry_t *pde;
pde = pmap_pde(pmap, vcur);
if (0 == pde || ((*pde & INTEL_PTE_VALID) == 0)) {
/* Make sure we wouldn't overflow */
if (vcur < (end - NBPD)) {
vincr = NBPD;
}
}
}
#endif /* defined(__x86_64__) */
}
vcur += vincr;
}
if ((ret == KERN_SUCCESS) && lastvavalid) {
/* send previous run */
ret = callback(vcurstart, vcur, context);
}
return (ret);
}
struct kern_dump_preflight_context
{
uint32_t region_count;
uint64_t dumpable_bytes;
};
int
kern_dump_pmap_traverse_preflight_callback(vm_map_offset_t start,
vm_map_offset_t end,
void *context)
{
struct kern_dump_preflight_context *kdc = (struct kern_dump_preflight_context *)context;
IOReturn ret = KERN_SUCCESS;
kdc->region_count++;
kdc->dumpable_bytes += (end - start);
return (ret);
}
struct kern_dump_send_seg_desc_context
{
core_save_segment_descriptions_cb callback;
void *context;
};
int
kern_dump_pmap_traverse_send_segdesc_callback(vm_map_offset_t start,
vm_map_offset_t end,
void *context)
{
struct kern_dump_send_seg_desc_context *kds_context = (struct kern_dump_send_seg_desc_context *)context;
uint64_t seg_start = (uint64_t) start;
uint64_t seg_end = (uint64_t) end;
return kds_context->callback(seg_start, seg_end, kds_context->context);
}
struct kern_dump_send_segdata_context
{
core_save_segment_data_cb callback;
void *context;
};
int
kern_dump_pmap_traverse_send_segdata_callback(vm_map_offset_t start,
vm_map_offset_t end,
void *context)
{
struct kern_dump_send_segdata_context *kds_context = (struct kern_dump_send_segdata_context *)context;
return kds_context->callback((void *)start, (uint64_t)(end - start), kds_context->context);
}
static int
kern_dump_save_summary(__unused void *refcon, core_save_summary_cb callback, void *context)
{
struct kern_dump_preflight_context kdc_preflight = { };
uint64_t thread_state_size = 0, thread_count = 0;
kern_return_t ret;
ret = pmap_traverse_present_mappings(kernel_pmap,
VM_MIN_KERNEL_AND_KEXT_ADDRESS,
VM_MAX_KERNEL_ADDRESS,
kern_dump_pmap_traverse_preflight_callback,
&kdc_preflight);
if (ret != KERN_SUCCESS) {
kern_coredump_log(context, "save_summary: pmap traversal failed: %d\n", ret);
return ret;
}
kern_collectth_state_size(&thread_count, &thread_state_size);
ret = callback(kdc_preflight.region_count, kdc_preflight.dumpable_bytes,
thread_count, thread_state_size, 0, context);
return ret;
}
static int
kern_dump_save_seg_descriptions(__unused void *refcon, core_save_segment_descriptions_cb callback, void *context)
{
kern_return_t ret;
struct kern_dump_send_seg_desc_context kds_context;
kds_context.callback = callback;
kds_context.context = context;
ret = pmap_traverse_present_mappings(kernel_pmap,
VM_MIN_KERNEL_AND_KEXT_ADDRESS,
VM_MAX_KERNEL_ADDRESS,
kern_dump_pmap_traverse_send_segdesc_callback,
&kds_context);
if (ret != KERN_SUCCESS) {
kern_coredump_log(context, "save_seg_desc: pmap traversal failed: %d\n", ret);
return ret;
}
return KERN_SUCCESS;
}
static int
kern_dump_save_thread_state(__unused void *refcon, void *buf, core_save_thread_state_cb callback, void *context)
{
kern_return_t ret;
uint64_t thread_state_size = 0, thread_count = 0;
kern_collectth_state_size(&thread_count, &thread_state_size);
if (thread_state_size > 0) {
void * iter = NULL;
do {
kern_collectth_state (current_thread(), buf, thread_state_size, &iter);
ret = callback(buf, context);
if (ret != KERN_SUCCESS) {
return ret;
}
} while (iter);
}
return KERN_SUCCESS;
}
static int
kern_dump_save_sw_vers(__unused void *refcon, core_save_sw_vers_cb callback, void *context)
{
return callback(&kdp_kernelversion_string, sizeof(kdp_kernelversion_string), context);
}
static int
kern_dump_save_segment_data(__unused void *refcon, core_save_segment_data_cb callback, void *context)
{