Loading...
  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
/*
 * Copyright (c) 2007 Apple 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@
 */
#include <debug.h>
#include <mach/mach_types.h>
#include <mach/kern_return.h>
#include <mach/thread_status.h>
#include <kern/thread.h>
#include <kern/kalloc.h>
#include <arm/vmparam.h>
#include <arm/cpu_data_internal.h>
#include <arm/proc_reg.h>

struct arm_vfpv2_state
{
        __uint32_t        __r[32];
        __uint32_t        __fpscr;

};

typedef struct arm_vfpv2_state	arm_vfpv2_state_t;

#define	ARM_VFPV2_STATE_COUNT ((mach_msg_type_number_t) \
	(sizeof (arm_vfpv2_state_t)/sizeof(uint32_t)))


/*
 * Forward definitions
 */
void
                thread_set_child(thread_t child, int pid);

void
                thread_set_parent(thread_t parent, int pid);

/*
 * Maps state flavor to number of words in the state:
 */
/* __private_extern__ */
unsigned int    _MachineStateCount[] = {
	 /* FLAVOR_LIST */ 0,
	ARM_THREAD_STATE_COUNT,
	ARM_VFP_STATE_COUNT,
	ARM_EXCEPTION_STATE_COUNT,
	ARM_DEBUG_STATE_COUNT
};

extern zone_t ads_zone;

kern_return_t
machine_thread_state_convert_to_user(
			 __unused thread_t thread,
			 __unused thread_flavor_t flavor,
			 __unused thread_state_t tstate,
			 __unused mach_msg_type_number_t *count)
{
	// No conversion to userspace representation on this platform
	return KERN_SUCCESS;
}

kern_return_t
machine_thread_state_convert_from_user(
			 __unused thread_t thread,
			 __unused thread_flavor_t flavor,
			 __unused thread_state_t tstate,
			 __unused mach_msg_type_number_t count)
{
	// No conversion from userspace representation on this platform
	return KERN_SUCCESS;
}

kern_return_t
machine_thread_siguctx_pointer_convert_to_user(
			 __unused thread_t thread,
			 __unused user_addr_t *uctxp)
{
	// No conversion to userspace representation on this platform
	return KERN_SUCCESS;
}

kern_return_t
machine_thread_function_pointers_convert_from_user(
			 __unused thread_t thread,
			 __unused user_addr_t *fptrs,
			 __unused uint32_t count)
{
	// No conversion from userspace representation on this platform
	return KERN_SUCCESS;
}

/*
 * Routine:	machine_thread_get_state
 *
 */
kern_return_t
machine_thread_get_state(
			 thread_t thread,
			 thread_flavor_t flavor,
			 thread_state_t tstate,
			 mach_msg_type_number_t * count)
{

#define machine_thread_get_state_kprintf(x...)	/* kprintf("machine_thread_get
						 * _state: " x) */

	switch (flavor) {
	case THREAD_STATE_FLAVOR_LIST:
		if (*count < 4)
			return (KERN_INVALID_ARGUMENT);

		tstate[0] = ARM_THREAD_STATE;
		tstate[1] = ARM_VFP_STATE;
		tstate[2] = ARM_EXCEPTION_STATE;
		tstate[3] = ARM_DEBUG_STATE;
		*count = 4;
		break;

	case ARM_THREAD_STATE:{
			struct arm_thread_state *state;
			struct arm_saved_state *saved_state;
			arm_unified_thread_state_t *unified_state;

			unsigned int    i;
			if (*count < ARM_THREAD_STATE_COUNT)
				return (KERN_INVALID_ARGUMENT);

			if (*count == ARM_UNIFIED_THREAD_STATE_COUNT) {
				unified_state = (arm_unified_thread_state_t *) tstate;
				state = &unified_state->ts_32;
				unified_state->ash.flavor = ARM_THREAD_STATE32;
				unified_state->ash.count = ARM_THREAD_STATE32_COUNT;
			} else {
				state = (struct arm_thread_state *) tstate;
			}
			saved_state = &thread->machine.PcbData;

			state->sp = saved_state->sp;
			state->lr = saved_state->lr;
			state->pc = saved_state->pc;
			state->cpsr = saved_state->cpsr;
			for (i = 0; i < 13; i++)
				state->r[i] = saved_state->r[i];
			machine_thread_get_state_kprintf("machine_thread_get_state: pc 0x%x r0 0x%x sp  0x%x\n",
					 state->pc, state->r[0], state->sp);

			if (*count != ARM_UNIFIED_THREAD_STATE_COUNT) {
				*count = ARM_THREAD_STATE_COUNT;
			}
			break;
		}
	case ARM_EXCEPTION_STATE:{
			struct arm_exception_state *state;
			struct arm_saved_state *saved_state;

			if (*count < ARM_EXCEPTION_STATE_COUNT)
				return (KERN_INVALID_ARGUMENT);

			state = (struct arm_exception_state *) tstate;
			saved_state = &thread->machine.PcbData;

			state->exception = saved_state->exception;
			state->fsr = saved_state->fsr;
			state->far = saved_state->far;

			*count = ARM_EXCEPTION_STATE_COUNT;
			break;
		}
	case ARM_VFP_STATE:{
#if	__ARM_VFP__
			struct arm_vfp_state *state;
			struct arm_vfpsaved_state *saved_state;
			unsigned int    i;
			unsigned int	max;

			if (*count < ARM_VFP_STATE_COUNT) {
				if (*count < ARM_VFPV2_STATE_COUNT)
					return (KERN_INVALID_ARGUMENT);
				else
					*count =  ARM_VFPV2_STATE_COUNT;
			}

			if (*count ==  ARM_VFPV2_STATE_COUNT)
				max = 32;
			else
				max = 64;

			state = (struct arm_vfp_state *) tstate;
			saved_state = find_user_vfp(thread);

			state->fpscr = saved_state->fpscr;
			for (i = 0; i < max; i++)
				state->r[i] = saved_state->r[i];

#endif
			break;
		}
	case ARM_DEBUG_STATE:{
			arm_debug_state_t *state;
			arm_debug_state_t *thread_state;

                        if (*count < ARM_DEBUG_STATE_COUNT)
				return (KERN_INVALID_ARGUMENT);
			
                        state = (arm_debug_state_t *) tstate;
                        thread_state = find_debug_state(thread);
                        
                        if (thread_state == NULL)
				bzero(state, sizeof(arm_debug_state_t));
                        else
				bcopy(thread_state, state, sizeof(arm_debug_state_t));
			
                        *count = ARM_DEBUG_STATE_COUNT;
                        break;
		}

	default:
		return (KERN_INVALID_ARGUMENT);
	}
	return (KERN_SUCCESS);
}


/*
 * Routine:	machine_thread_get_kern_state
 *
 */
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)
{

#define machine_thread_get_kern_state_kprintf(x...)	/* kprintf("machine_threa
							 * d_get_kern_state: "
							 * x) */

	/*
	 * This works only for an interrupted kernel thread
	 */
	if (thread != current_thread() || getCpuDatap()->cpu_int_state == NULL)
		return KERN_FAILURE;

	switch (flavor) {
	case ARM_THREAD_STATE:{
			struct arm_thread_state *state;
			struct arm_saved_state *saved_state;
			unsigned int    i;
			if (*count < ARM_THREAD_STATE_COUNT)
				return (KERN_INVALID_ARGUMENT);

			state = (struct arm_thread_state *) tstate;
			saved_state = getCpuDatap()->cpu_int_state;

			state->sp = saved_state->sp;
			state->lr = saved_state->lr;
			state->pc = saved_state->pc;
			state->cpsr = saved_state->cpsr;
			for (i = 0; i < 13; i++)
				state->r[i] = saved_state->r[i];
			machine_thread_get_kern_state_kprintf("machine_thread_get_state: pc 0x%x r0 0x%x sp  0x%x\n",
					 state->pc, state->r[0], state->sp);
			*count = ARM_THREAD_STATE_COUNT;
			break;
		}
	default:
		return (KERN_INVALID_ARGUMENT);
	}
	return (KERN_SUCCESS);
}

extern long long arm_debug_get(void);

/*
 * Routine:	machine_thread_set_state
 *
 */
kern_return_t
machine_thread_set_state(
			 thread_t thread,
			 thread_flavor_t flavor,
			 thread_state_t tstate,
			 mach_msg_type_number_t count)
{

#define machine_thread_set_state_kprintf(x...)	/* kprintf("machine_thread_set
						 * _state: " x) */

	switch (flavor) {
	case ARM_THREAD_STATE:{
			struct arm_thread_state *state;
			struct arm_saved_state *saved_state;
			arm_unified_thread_state_t *unified_state;
			int             old_psr;

			if (count < ARM_THREAD_STATE_COUNT)
				return (KERN_INVALID_ARGUMENT);

			if (count == ARM_UNIFIED_THREAD_STATE_COUNT) {
				unified_state = (arm_unified_thread_state_t *) tstate;
				state = &unified_state->ts_32;
			} else {
				state = (struct arm_thread_state *) tstate;
			}
			saved_state = &thread->machine.PcbData;
			old_psr = saved_state->cpsr;
			memcpy((char *) saved_state, (char *) state, sizeof(*state));
			/*
			 * do not allow privileged bits of the PSR to be
			 * changed
			 */
			saved_state->cpsr = (saved_state->cpsr & ~PSR_USER_MASK) | (old_psr & PSR_USER_MASK);

			machine_thread_set_state_kprintf("machine_thread_set_state: pc 0x%x r0 0x%x sp 0x%x\n",
					 state->pc, state->r[0], state->sp);
			break;
		}
	case ARM_VFP_STATE:{
#if __ARM_VFP__
			struct arm_vfp_state *state;
			struct arm_vfpsaved_state *saved_state;
			unsigned int    i;
			unsigned int	max;

			if (count < ARM_VFP_STATE_COUNT) {
				if (count < ARM_VFPV2_STATE_COUNT)
					return (KERN_INVALID_ARGUMENT);
				else
					count =  ARM_VFPV2_STATE_COUNT;
			}

			if (count ==  ARM_VFPV2_STATE_COUNT)
				max = 32;
			else
				max = 64;

			state = (struct arm_vfp_state *) tstate;
			saved_state = find_user_vfp(thread);

			saved_state->fpscr = state->fpscr;
			for (i = 0; i < max; i++)
				saved_state->r[i] = state->r[i];

#endif
			break;
		}
	case ARM_EXCEPTION_STATE:{

			if (count < ARM_EXCEPTION_STATE_COUNT)
				return (KERN_INVALID_ARGUMENT);

			break;
		}
	case ARM_DEBUG_STATE:{
			arm_debug_state_t *state;
			arm_debug_state_t *thread_state;
                        boolean_t enabled = FALSE;
			unsigned int    i;

                        if (count < ARM_DEBUG_STATE_COUNT)
				return (KERN_INVALID_ARGUMENT);

                        state = (arm_debug_state_t *) tstate;
                        thread_state = find_debug_state(thread);

			if (count < ARM_DEBUG_STATE_COUNT)
				return (KERN_INVALID_ARGUMENT);
			
                        for (i = 0; i < 16; i++) {
				/* do not allow context IDs to be set */
				if (((state->bcr[i] & ARM_DBGBCR_TYPE_MASK) != ARM_DBGBCR_TYPE_IVA)
				    || ((state->bcr[i] & ARM_DBG_CR_LINKED_MASK) != ARM_DBG_CR_LINKED_UNLINKED)
				    || ((state->wcr[i] & ARM_DBGBCR_TYPE_MASK) != ARM_DBGBCR_TYPE_IVA)
				    || ((state->wcr[i] & ARM_DBG_CR_LINKED_MASK) != ARM_DBG_CR_LINKED_UNLINKED)) {
					return KERN_PROTECTION_FAILURE;
				}
				if ((((state->bcr[i] & ARM_DBG_CR_ENABLE_MASK) == ARM_DBG_CR_ENABLE_ENABLE))
				    || ((state->wcr[i] & ARM_DBG_CR_ENABLE_MASK) == ARM_DBG_CR_ENABLE_ENABLE)) {
					enabled = TRUE;
				}
                        }
			
                        if (!enabled) {
				if (thread_state != NULL)
				{
                                        void *pTmp = thread->machine.DebugData;
                                        thread->machine.DebugData = NULL;
                                        zfree(ads_zone, pTmp);
				}
                        }
                        else
                        {
				if (thread_state == NULL)
					thread_state = zalloc(ads_zone);
				
				for (i = 0; i < 16; i++) {
					/* set appropriate priviledge; mask out unknown bits */
					thread_state->bcr[i] = (state->bcr[i] & (ARM_DBG_CR_ADDRESS_MASK_MASK
										     | ARM_DBGBCR_MATCH_MASK
										     | ARM_DBG_CR_BYTE_ADDRESS_SELECT_MASK
										     | ARM_DBG_CR_ENABLE_MASK))
						| ARM_DBGBCR_TYPE_IVA
						| ARM_DBG_CR_LINKED_UNLINKED
						| ARM_DBG_CR_SECURITY_STATE_BOTH
						| ARM_DBG_CR_MODE_CONTROL_USER;
					thread_state->bvr[i] = state->bvr[i] & ARM_DBG_VR_ADDRESS_MASK;
					thread_state->wcr[i] = (state->wcr[i] & (ARM_DBG_CR_ADDRESS_MASK_MASK
										     | ARM_DBGWCR_BYTE_ADDRESS_SELECT_MASK
										     | ARM_DBGWCR_ACCESS_CONTROL_MASK
										     | ARM_DBG_CR_ENABLE_MASK))
						| ARM_DBG_CR_LINKED_UNLINKED
						| ARM_DBG_CR_SECURITY_STATE_BOTH
						| ARM_DBG_CR_MODE_CONTROL_USER;                                
					thread_state->wvr[i] = state->wvr[i] & ARM_DBG_VR_ADDRESS_MASK;
				}
				
				if (thread->machine.DebugData == NULL)
					thread->machine.DebugData = thread_state;
                        }
			
                        if (thread == current_thread()) {
                                arm_debug_set(thread_state);
			}
			
			break;
		}
        
	default:
		return (KERN_INVALID_ARGUMENT);
	}
	return (KERN_SUCCESS);
}

/*
 * Routine:	machine_thread_state_initialize
 *
 */
kern_return_t
machine_thread_state_initialize(
				thread_t thread)
{
	struct arm_saved_state *savestate;

	savestate = (struct arm_saved_state *) & thread->machine.PcbData;
	bzero((char *) savestate, sizeof(struct arm_saved_state));
	savestate->cpsr = PSR_USERDFLT;

#if __ARM_VFP__
	vfp_state_initialize(&thread->machine.uVFPdata);
	vfp_state_initialize(&thread->machine.kVFPdata);
#endif

	thread->machine.DebugData = NULL;

	return KERN_SUCCESS;
}

#if __ARM_VFP__
void
vfp_state_initialize(struct arm_vfpsaved_state *vfp_state)
{
	/* Set default VFP state to RunFast mode:
	*
	* - flush-to-zero mode
	* - default NaN mode
	* - no enabled exceptions
	*
	* On the VFP11, this allows the use of floating point without
	* trapping to support code, which we do not provide.  With
	* the Cortex-A8, this allows the use of the (much faster) NFP
	* pipeline for single-precision operations.
	*/

	bzero(vfp_state, sizeof(*vfp_state));
	vfp_state->fpscr = FPSCR_DEFAULT;
}
#endif /* __ARM_VFP__ */


/*
 * Routine:	machine_thread_dup
 *
 */
kern_return_t
machine_thread_dup(
		   thread_t self,
		   thread_t target,
		   __unused boolean_t is_corpse)
{
	struct arm_saved_state *self_saved_state;
	struct arm_saved_state *target_saved_state;

#if	__ARM_VFP__
	struct arm_vfpsaved_state *self_vfp_state;
	struct arm_vfpsaved_state *target_vfp_state;
#endif

	target->machine.cthread_self = self->machine.cthread_self;
	target->machine.cthread_data = self->machine.cthread_data;

	self_saved_state = &self->machine.PcbData;
	target_saved_state = &target->machine.PcbData;
	bcopy(self_saved_state, target_saved_state, sizeof(struct arm_saved_state));

#if	__ARM_VFP__
	self_vfp_state = &self->machine.uVFPdata;
	target_vfp_state = &target->machine.uVFPdata;
	bcopy(self_vfp_state, target_vfp_state, sizeof(struct arm_vfpsaved_state));
#endif

	return (KERN_SUCCESS);
}

/*
 * Routine:	get_user_regs
 *
 */
struct arm_saved_state *
get_user_regs(
	      thread_t thread)
{
	return (&thread->machine.PcbData);
}

/*
 * Routine:	find_user_regs
 *
 */
struct arm_saved_state *
find_user_regs(
	       thread_t thread)
{
	return get_user_regs(thread);
}

/*
 * Routine:	find_kern_regs
 *
 */
struct arm_saved_state *
find_kern_regs(
	       thread_t thread)
{
	/*
         * This works only for an interrupted kernel thread
         */
	if (thread != current_thread() || getCpuDatap()->cpu_int_state == NULL)
		return ((struct arm_saved_state *) NULL);
	else
		return (getCpuDatap()->cpu_int_state);

}

#if __ARM_VFP__
/*
 *	Find the user state floating point context.  If there is no user state context,
 *	we just return a 0.
 */

struct arm_vfpsaved_state *
find_user_vfp(
	      thread_t thread)
{
	return &thread->machine.uVFPdata;
}
#endif /* __ARM_VFP__ */

arm_debug_state_t *
find_debug_state(
             thread_t thread)
{
       return thread->machine.DebugData;
}

/*
 * Routine:	thread_userstack
 *
 */
kern_return_t
thread_userstack(
		 __unused thread_t thread,
		 int flavor,
		 thread_state_t tstate,
		 unsigned int count,
		 mach_vm_offset_t * user_stack,
		 int *customstack,
		 __unused boolean_t is64bit
)
{

	switch (flavor) {
	case ARM_THREAD_STATE:
		{
			struct arm_thread_state *state;


			if (count < ARM_THREAD_STATE_COUNT)
				return (KERN_INVALID_ARGUMENT);

			if (customstack)
				*customstack = 0;
			state = (struct arm_thread_state *) tstate;

			if (state->sp) {
				*user_stack = CAST_USER_ADDR_T(state->sp);
				if (customstack)
					*customstack = 1;
			} else {
				*user_stack = CAST_USER_ADDR_T(USRSTACK);
			}
		}
		break;

	default:
		return (KERN_INVALID_ARGUMENT);
	}

	return (KERN_SUCCESS);
}

/*
 * thread_userstackdefault:
 *
 * Return the default stack location for the
 * thread, if otherwise unknown.
 */
kern_return_t
thread_userstackdefault(
	mach_vm_offset_t *default_user_stack,
	boolean_t is64bit __unused)
{
	*default_user_stack = USRSTACK;

	return (KERN_SUCCESS);
}

/*
 * Routine:	thread_setuserstack
 *
 */
void
thread_setuserstack(thread_t thread, mach_vm_address_t user_stack)
{
	struct arm_saved_state *sv;

#define thread_setuserstack_kprintf(x...)	/* kprintf("thread_setuserstac
						 * k: " x) */

	sv = get_user_regs(thread);

	sv->sp = user_stack;

	thread_setuserstack_kprintf("stack %x\n", sv->sp);

	return;
}

/*
 * Routine:	thread_adjuserstack
 *
 */
uint64_t
thread_adjuserstack(thread_t thread, int adjust)
{
	struct arm_saved_state *sv;

	sv = get_user_regs(thread);

	sv->sp += adjust;

	return sv->sp;
}

/*
 * Routine:	thread_setentrypoint
 *
 */
void
thread_setentrypoint(thread_t thread, mach_vm_offset_t entry)
{
	struct arm_saved_state *sv;

#define thread_setentrypoint_kprintf(x...)	/* kprintf("thread_setentrypoi
						 * nt: " x) */

	sv = get_user_regs(thread);

	sv->pc = entry;

	thread_setentrypoint_kprintf("entry %x\n", sv->pc);

	return;
}

/*
 * Routine:	thread_entrypoint
 *
 */
kern_return_t
thread_entrypoint(
		  __unused thread_t thread,
		  int flavor,
		  thread_state_t tstate,
		  __unused unsigned int count,
		  mach_vm_offset_t * entry_point
)
{
	switch (flavor) {
	case ARM_THREAD_STATE:
		{
			struct arm_thread_state *state;

			state = (struct arm_thread_state *) tstate;

			/*
			 * If a valid entry point is specified, use it.
			 */
			if (state->pc) {
				*entry_point = CAST_USER_ADDR_T(state->pc);
			} else {
				*entry_point = CAST_USER_ADDR_T(VM_MIN_ADDRESS);
			}
		}
		break;

	default:
		return (KERN_INVALID_ARGUMENT);
	}

	return (KERN_SUCCESS);
}


/*
 * Routine:	thread_set_child
 *
 */
void
thread_set_child(
		 thread_t child,
		 int pid)
{
	struct arm_saved_state *child_state;

	child_state = get_user_regs(child);

	child_state->r[0] = (uint_t) pid;
	child_state->r[1] = 1ULL;
}


/*
 * Routine:	thread_set_parent
 *
 */
void
thread_set_parent(
		  thread_t parent,
		  int pid)
{
	struct arm_saved_state *parent_state;

	parent_state = get_user_regs(parent);

	parent_state->r[0] = pid;
	parent_state->r[1] = 0;
}


struct arm_act_context {
	struct arm_saved_state ss;
#if __ARM_VFP__
	struct arm_vfpsaved_state vfps;
#endif
};

/*
 * Routine:	act_thread_csave
 *
 */
void           *
act_thread_csave(void)
{
	struct arm_act_context *ic;
	kern_return_t   kret;
	unsigned int    val;

	ic = (struct arm_act_context *) kalloc(sizeof(struct arm_act_context));

	if (ic == (struct arm_act_context *) NULL)
		return ((void *) 0);

	val = ARM_THREAD_STATE_COUNT;
	kret = machine_thread_get_state(current_thread(),
					ARM_THREAD_STATE,
					(thread_state_t) & ic->ss,
					&val);
	if (kret != KERN_SUCCESS) {
		kfree(ic, sizeof(struct arm_act_context));
		return ((void *) 0);
	}
#if __ARM_VFP__
	val = ARM_VFP_STATE_COUNT;
	kret = machine_thread_get_state(current_thread(),
					ARM_VFP_STATE,
					(thread_state_t) & ic->vfps,
					&val);
	if (kret != KERN_SUCCESS) {
		kfree(ic, sizeof(struct arm_act_context));
		return ((void *) 0);
	}
#endif
	return (ic);
}

/*
 * Routine:	act_thread_catt
 *
 */
void
act_thread_catt(void *ctx)
{
	struct arm_act_context *ic;
	kern_return_t   kret;

	ic = (struct arm_act_context *) ctx;

	if (ic == (struct arm_act_context *) NULL)
		return;

	kret = machine_thread_set_state(current_thread(),
					ARM_THREAD_STATE,
					(thread_state_t) & ic->ss,
					ARM_THREAD_STATE_COUNT);
	if (kret != KERN_SUCCESS)
		goto out;

#if __ARM_VFP__
	kret = machine_thread_set_state(current_thread(),
					ARM_VFP_STATE,
					(thread_state_t) & ic->vfps,
					ARM_VFP_STATE_COUNT);
	if (kret != KERN_SUCCESS)
		goto out;
#endif
out:
	kfree(ic, sizeof(struct arm_act_context));
}

/*
 * Routine:	act_thread_catt
 *
 */
void 
act_thread_cfree(void *ctx)
{
	kfree(ctx, sizeof(struct arm_act_context));
}

kern_return_t
thread_set_wq_state32(thread_t thread, thread_state_t tstate)
{
	arm_thread_state_t *state;
	struct arm_saved_state *saved_state;
	thread_t curth = current_thread();
	spl_t s=0;

	saved_state = &thread->machine.PcbData;
	state = (arm_thread_state_t *)tstate;

	if (curth != thread) {
		s = splsched();
		thread_lock(thread);
	}

	/*
	 * do not zero saved_state, it can be concurrently accessed
	 * and zero is not a valid state for some of the registers,
	 * like sp.
	 */
	thread_state32_to_saved_state(state, saved_state);
	saved_state->cpsr = PSR_USERDFLT;

	if (curth != thread) {
		thread_unlock(thread);
		splx(s);
	}

	return KERN_SUCCESS;
}