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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
|
/*
* Mach Operating System
* Copyright (c) 1993-1988 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
/*
* processor.c: processor and processor_set manipulation routines.
*/
#include <string.h>
#include <mach/boolean.h>
#include <mach/policy.h>
#include <mach/processor_info.h>
#include <mach/vm_param.h>
#include <kern/cpu_number.h>
#include <kern/debug.h>
#include <kern/kalloc.h>
#include <kern/lock.h>
#include <kern/host.h>
#include <kern/ipc_tt.h>
#include <kern/processor.h>
#include <kern/sched.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <kern/ipc_host.h>
#include <ipc/ipc_port.h>
#if MACH_HOST
#include <kern/slab.h>
struct kmem_cache pset_cache;
#endif /* MACH_HOST */
/*
* Exported variables.
*/
struct processor_set default_pset;
struct processor processor_array[NCPUS];
queue_head_t all_psets;
int all_psets_count;
struct lock all_psets_lock_data;
processor_t master_processor;
processor_t processor_ptr[NCPUS];
/*
* Bootstrap the processor/pset system so the scheduler can run.
*/
void pset_sys_bootstrap(void)
{
int i;
pset_init(&default_pset);
default_pset.empty = FALSE;
for (i = 0; i < NCPUS; i++) {
/*
* Initialize processor data structures.
* Note that cpu_to_processor(i) is processor_ptr[i].
*/
processor_ptr[i] = &processor_array[i];
processor_init(processor_ptr[i], i);
}
master_processor = cpu_to_processor(master_cpu);
queue_init(&all_psets);
all_psets_lock_init();
queue_enter(&all_psets, &default_pset, processor_set_t, all_psets);
all_psets_count = 1;
default_pset.active = TRUE;
default_pset.empty = FALSE;
/*
* Note: the default_pset has a max_priority of BASEPRI_USER.
* Internal kernel threads override this in kernel_thread.
*/
}
#if MACH_HOST
/*
* Rest of pset system initializations.
*/
void pset_sys_init(void)
{
int i;
processor_t processor;
/*
* Allocate the cache for processor sets.
*/
kmem_cache_init(&pset_cache, "processor_set",
sizeof(struct processor_set), 0, NULL, NULL, NULL, 0);
/*
* Give each processor a control port.
* The master processor already has one.
*/
for (i = 0; i < NCPUS; i++) {
processor = cpu_to_processor(i);
if (processor != master_processor &&
machine_slot[i].is_cpu)
{
ipc_processor_init(processor);
}
}
}
#endif /* MACH_HOST */
/*
* Initialize the given processor_set structure.
*/
void pset_init(
processor_set_t pset)
{
int i;
simple_lock_init(&pset->runq.lock);
pset->runq.low = 0;
pset->runq.count = 0;
for (i = 0; i < NRQS; i++) {
queue_init(&(pset->runq.runq[i]));
}
queue_init(&pset->idle_queue);
pset->idle_count = 0;
simple_lock_init(&pset->idle_lock);
queue_init(&pset->processors);
pset->processor_count = 0;
pset->empty = TRUE;
queue_init(&pset->tasks);
pset->task_count = 0;
queue_init(&pset->threads);
pset->thread_count = 0;
pset->ref_count = 1;
simple_lock_init(&pset->ref_lock);
queue_init(&pset->all_psets);
pset->active = FALSE;
pset_lock_init (pset);
pset->pset_self = IP_NULL;
pset->pset_name_self = IP_NULL;
pset->max_priority = BASEPRI_USER;
#if MACH_FIXPRI
pset->policies = POLICY_TIMESHARE;
#endif /* MACH_FIXPRI */
pset->set_quantum = min_quantum;
#if NCPUS > 1
pset->quantum_adj_index = 0;
simple_lock_init(&pset->quantum_adj_lock);
for (i = 0; i <= NCPUS; i++) {
pset->machine_quantum[i] = min_quantum;
}
#endif /* NCPUS > 1 */
pset->mach_factor = 0;
pset->load_average = 0;
pset->sched_load = SCHED_SCALE; /* i.e. 1 */
}
/*
* Initialize the given processor structure for the processor in
* the slot specified by slot_num.
*/
void processor_init(
processor_t pr,
int slot_num)
{
int i;
simple_lock_init(&pr->runq.lock);
pr->runq.low = 0;
pr->runq.count = 0;
for (i = 0; i < NRQS; i++) {
queue_init(&(pr->runq.runq[i]));
}
queue_init(&pr->processor_queue);
pr->state = PROCESSOR_OFF_LINE;
pr->next_thread = THREAD_NULL;
pr->idle_thread = THREAD_NULL;
pr->quantum = 0;
pr->first_quantum = FALSE;
pr->last_quantum = 0;
pr->processor_set = PROCESSOR_SET_NULL;
pr->processor_set_next = PROCESSOR_SET_NULL;
queue_init(&pr->processors);
simple_lock_init(&pr->lock);
pr->processor_self = IP_NULL;
pr->slot_num = slot_num;
}
/*
* pset_remove_processor() removes a processor from a processor_set.
* It can only be called on the current processor. Caller must
* hold lock on current processor and processor set.
*/
void pset_remove_processor(
processor_set_t pset,
processor_t processor)
{
if (pset != processor->processor_set)
panic("pset_remove_processor: wrong pset");
queue_remove(&pset->processors, processor, processor_t, processors);
processor->processor_set = PROCESSOR_SET_NULL;
pset->processor_count--;
quantum_set(pset);
}
/*
* pset_add_processor() adds a processor to a processor_set.
* It can only be called on the current processor. Caller must
* hold lock on curent processor and on pset. No reference counting on
* processors. Processor reference to pset is implicit.
*/
void pset_add_processor(
processor_set_t pset,
processor_t processor)
{
queue_enter(&pset->processors, processor, processor_t, processors);
processor->processor_set = pset;
pset->processor_count++;
quantum_set(pset);
}
/*
* pset_remove_task() removes a task from a processor_set.
* Caller must hold locks on pset and task. Pset reference count
* is not decremented; caller must explicitly pset_deallocate.
*/
void pset_remove_task(
processor_set_t pset,
task_t task)
{
if (pset != task->processor_set)
return;
queue_remove(&pset->tasks, task, task_t, pset_tasks);
task->processor_set = PROCESSOR_SET_NULL;
pset->task_count--;
}
/*
* pset_add_task() adds a task to a processor_set.
* Caller must hold locks on pset and task. Pset references to
* tasks are implicit.
*/
void pset_add_task(
processor_set_t pset,
task_t task)
{
queue_enter(&pset->tasks, task, task_t, pset_tasks);
task->processor_set = pset;
pset->task_count++;
}
/*
* pset_remove_thread() removes a thread from a processor_set.
* Caller must hold locks on pset and thread. Pset reference count
* is not decremented; caller must explicitly pset_deallocate.
*/
void pset_remove_thread(
processor_set_t pset,
thread_t thread)
{
queue_remove(&pset->threads, thread, thread_t, pset_threads);
thread->processor_set = PROCESSOR_SET_NULL;
pset->thread_count--;
}
/*
* pset_add_thread() adds a thread to a processor_set.
* Caller must hold locks on pset and thread. Pset references to
* threads are implicit.
*/
void pset_add_thread(
processor_set_t pset,
thread_t thread)
{
queue_enter(&pset->threads, thread, thread_t, pset_threads);
thread->processor_set = pset;
pset->thread_count++;
}
/*
* thread_change_psets() changes the pset of a thread. Caller must
* hold locks on both psets and thread. The old pset must be
* explicitly pset_deallocat()'ed by caller.
*/
void thread_change_psets(
thread_t thread,
processor_set_t old_pset,
processor_set_t new_pset)
{
queue_remove(&old_pset->threads, thread, thread_t, pset_threads);
old_pset->thread_count--;
queue_enter(&new_pset->threads, thread, thread_t, pset_threads);
thread->processor_set = new_pset;
new_pset->thread_count++;
}
/*
* pset_deallocate:
*
* Remove one reference to the processor set. Destroy processor_set
* if this was the last reference.
*/
void pset_deallocate(
processor_set_t pset)
{
if (pset == PROCESSOR_SET_NULL)
return;
pset_ref_lock(pset);
if (--pset->ref_count > 0) {
pset_ref_unlock(pset);
return;
}
#if !MACH_HOST
panic("pset_deallocate: default_pset destroyed");
#endif /* !MACH_HOST */
#if MACH_HOST
/*
* Reference count is zero, however the all_psets list
* holds an implicit reference and may make new ones.
* Its lock also dominates the pset lock. To check for this,
* temporarily restore one reference, and then lock the
* other structures in the right order.
*/
pset->ref_count = 1;
pset_ref_unlock(pset);
lock_all_psets();
pset_ref_lock(pset);
if (--pset->ref_count > 0) {
/*
* Made an extra reference.
*/
pset_ref_unlock(pset);
unlock_all_psets();
return;
}
/*
* Ok to destroy pset. Make a few paranoia checks.
*/
if ((pset == &default_pset) || (pset->thread_count > 0) ||
(pset->task_count > 0) || pset->processor_count > 0) {
panic("pset_deallocate: destroy default or active pset");
}
/*
* Remove from all_psets queue.
*/
queue_remove(&all_psets, pset, processor_set_t, all_psets);
all_psets_count--;
pset_ref_unlock(pset);
unlock_all_psets();
/*
* That's it, free data structure.
*/
kmem_cache_free(&pset_cache, (vm_offset_t)pset);
#endif /* MACH_HOST */
}
/*
* pset_reference:
*
* Add one reference to the processor set.
*/
void pset_reference(
processor_set_t pset)
{
pset_ref_lock(pset);
pset->ref_count++;
pset_ref_unlock(pset);
}
kern_return_t
processor_info(
processor_t processor,
int flavor,
host_t *host,
processor_info_t info,
natural_t *count)
{
int slot_num, state;
processor_basic_info_t basic_info;
if (processor == PROCESSOR_NULL)
return KERN_INVALID_ARGUMENT;
if (flavor != PROCESSOR_BASIC_INFO ||
*count < PROCESSOR_BASIC_INFO_COUNT)
return KERN_FAILURE;
basic_info = (processor_basic_info_t) info;
slot_num = processor->slot_num;
basic_info->cpu_type = machine_slot[slot_num].cpu_type;
basic_info->cpu_subtype = machine_slot[slot_num].cpu_subtype;
state = processor->state;
if (state == PROCESSOR_SHUTDOWN || state == PROCESSOR_OFF_LINE)
basic_info->running = FALSE;
else
basic_info->running = TRUE;
basic_info->slot_num = slot_num;
if (processor == master_processor)
basic_info->is_master = TRUE;
else
basic_info->is_master = FALSE;
*count = PROCESSOR_BASIC_INFO_COUNT;
*host = &realhost;
return KERN_SUCCESS;
}
kern_return_t processor_start(
processor_t processor)
{
if (processor == PROCESSOR_NULL)
return KERN_INVALID_ARGUMENT;
#if NCPUS > 1
return cpu_start(processor->slot_num);
#else /* NCPUS > 1 */
return KERN_FAILURE;
#endif /* NCPUS > 1 */
}
kern_return_t processor_exit(
processor_t processor)
{
if (processor == PROCESSOR_NULL)
return KERN_INVALID_ARGUMENT;
#if NCPUS > 1
return processor_shutdown(processor);
#else /* NCPUS > 1 */
return KERN_FAILURE;
#endif /* NCPUS > 1 */
}
kern_return_t
processor_control(
processor_t processor,
processor_info_t info,
natural_t count)
{
if (processor == PROCESSOR_NULL)
return KERN_INVALID_ARGUMENT;
#if NCPUS > 1
return cpu_control(processor->slot_num, (int *)info, count);
#else /* NCPUS > 1 */
return KERN_FAILURE;
#endif /* NCPUS > 1 */
}
/*
* Precalculate the appropriate system quanta based on load. The
* index into machine_quantum is the number of threads on the
* processor set queue. It is limited to the number of processors in
* the set.
*/
void quantum_set(
processor_set_t pset)
{
#if NCPUS > 1
int i, ncpus;
ncpus = pset->processor_count;
for ( i=1 ; i <= ncpus ; i++) {
pset->machine_quantum[i] =
((min_quantum * ncpus) + (i/2)) / i ;
}
pset->machine_quantum[0] = 2 * pset->machine_quantum[1];
i = ((pset->runq.count > pset->processor_count) ?
pset->processor_count : pset->runq.count);
pset->set_quantum = pset->machine_quantum[i];
#else /* NCPUS > 1 */
default_pset.set_quantum = min_quantum;
#endif /* NCPUS > 1 */
}
#if MACH_HOST
/*
* processor_set_create:
*
* Create and return a new processor set.
*/
kern_return_t
processor_set_create(
host_t host,
processor_set_t *new_set,
processor_set_t *new_name)
{
processor_set_t pset;
if (host == HOST_NULL)
return KERN_INVALID_ARGUMENT;
pset = (processor_set_t) kmem_cache_alloc(&pset_cache);
pset_init(pset);
pset_reference(pset); /* for new_set out argument */
pset_reference(pset); /* for new_name out argument */
ipc_pset_init(pset);
pset->active = TRUE;
lock_all_psets();
queue_enter(&all_psets, pset, processor_set_t, all_psets);
all_psets_count++;
unlock_all_psets();
ipc_pset_enable(pset);
*new_set = pset;
*new_name = pset;
return KERN_SUCCESS;
}
/*
* processor_set_destroy:
*
* destroy a processor set. Any tasks, threads or processors
* currently assigned to it are reassigned to the default pset.
*/
kern_return_t processor_set_destroy(
processor_set_t pset)
{
queue_entry_t elem;
queue_head_t *list;
if (pset == PROCESSOR_SET_NULL || pset == &default_pset)
return KERN_INVALID_ARGUMENT;
/*
* Handle multiple termination race. First one through sets
* active to FALSE and disables ipc access.
*/
pset_lock(pset);
if (!(pset->active)) {
pset_unlock(pset);
return KERN_FAILURE;
}
pset->active = FALSE;
ipc_pset_disable(pset);
/*
* Now reassign everything in this set to the default set.
*/
if (pset->task_count > 0) {
list = &pset->tasks;
while (!queue_empty(list)) {
elem = queue_first(list);
task_reference((task_t) elem);
pset_unlock(pset);
task_assign((task_t) elem, &default_pset, FALSE);
task_deallocate((task_t) elem);
pset_lock(pset);
}
}
if (pset->thread_count > 0) {
list = &pset->threads;
while (!queue_empty(list)) {
elem = queue_first(list);
thread_reference((thread_t) elem);
pset_unlock(pset);
thread_assign((thread_t) elem, &default_pset);
thread_deallocate((thread_t) elem);
pset_lock(pset);
}
}
if (pset->processor_count > 0) {
list = &pset->processors;
while(!queue_empty(list)) {
elem = queue_first(list);
pset_unlock(pset);
processor_assign((processor_t) elem, &default_pset, TRUE);
pset_lock(pset);
}
}
pset_unlock(pset);
/*
* Destroy ipc state.
*/
ipc_pset_terminate(pset);
/*
* Deallocate pset's reference to itself.
*/
pset_deallocate(pset);
return KERN_SUCCESS;
}
#else /* MACH_HOST */
kern_return_t
processor_set_create(
host_t host,
processor_set_t *new_set,
processor_set_t *new_name)
{
return KERN_FAILURE;
}
kern_return_t processor_set_destroy(
processor_set_t pset)
{
return KERN_FAILURE;
}
#endif /* MACH_HOST */
kern_return_t
processor_get_assignment(
processor_t processor,
processor_set_t *pset)
{
int state;
if (processor == PROCESSOR_NULL)
return KERN_INVALID_ARGUMENT;
state = processor->state;
if (state == PROCESSOR_SHUTDOWN || state == PROCESSOR_OFF_LINE)
return KERN_FAILURE;
*pset = processor->processor_set;
pset_reference(*pset);
return KERN_SUCCESS;
}
kern_return_t
processor_set_info(
processor_set_t pset,
int flavor,
host_t *host,
processor_set_info_t info,
natural_t *count)
{
if (pset == PROCESSOR_SET_NULL)
return KERN_INVALID_ARGUMENT;
if (flavor == PROCESSOR_SET_BASIC_INFO) {
processor_set_basic_info_t basic_info;
if (*count < PROCESSOR_SET_BASIC_INFO_COUNT)
return KERN_FAILURE;
basic_info = (processor_set_basic_info_t) info;
pset_lock(pset);
basic_info->processor_count = pset->processor_count;
basic_info->task_count = pset->task_count;
basic_info->thread_count = pset->thread_count;
basic_info->mach_factor = pset->mach_factor;
basic_info->load_average = pset->load_average;
pset_unlock(pset);
*count = PROCESSOR_SET_BASIC_INFO_COUNT;
*host = &realhost;
return KERN_SUCCESS;
}
else if (flavor == PROCESSOR_SET_SCHED_INFO) {
processor_set_sched_info_t sched_info;
if (*count < PROCESSOR_SET_SCHED_INFO_COUNT)
return KERN_FAILURE;
sched_info = (processor_set_sched_info_t) info;
pset_lock(pset);
#if MACH_FIXPRI
sched_info->policies = pset->policies;
#else /* MACH_FIXPRI */
sched_info->policies = POLICY_TIMESHARE;
#endif /* MACH_FIXPRI */
sched_info->max_priority = pset->max_priority;
pset_unlock(pset);
*count = PROCESSOR_SET_SCHED_INFO_COUNT;
*host = &realhost;
return KERN_SUCCESS;
}
*host = HOST_NULL;
return KERN_INVALID_ARGUMENT;
}
/*
* processor_set_max_priority:
*
* Specify max priority permitted on processor set. This affects
* newly created and assigned threads. Optionally change existing
* ones.
*/
kern_return_t
processor_set_max_priority(
processor_set_t pset,
int max_priority,
boolean_t change_threads)
{
if (pset == PROCESSOR_SET_NULL || invalid_pri(max_priority))
return KERN_INVALID_ARGUMENT;
pset_lock(pset);
pset->max_priority = max_priority;
if (change_threads) {
queue_head_t *list;
thread_t thread;
list = &pset->threads;
queue_iterate(list, thread, thread_t, pset_threads) {
if (thread->max_priority < max_priority)
thread_max_priority(thread, pset, max_priority);
}
}
pset_unlock(pset);
return KERN_SUCCESS;
}
/*
* processor_set_policy_enable:
*
* Allow indicated policy on processor set.
*/
kern_return_t
processor_set_policy_enable(
processor_set_t pset,
int policy)
{
if ((pset == PROCESSOR_SET_NULL) || invalid_policy(policy))
return KERN_INVALID_ARGUMENT;
#if MACH_FIXPRI
pset_lock(pset);
pset->policies |= policy;
pset_unlock(pset);
return KERN_SUCCESS;
#else /* MACH_FIXPRI */
if (policy == POLICY_TIMESHARE)
return KERN_SUCCESS;
else
return KERN_FAILURE;
#endif /* MACH_FIXPRI */
}
/*
* processor_set_policy_disable:
*
* Forbid indicated policy on processor set. Time sharing cannot
* be forbidden.
*/
kern_return_t
processor_set_policy_disable(
processor_set_t pset,
int policy,
boolean_t change_threads)
{
if ((pset == PROCESSOR_SET_NULL) || policy == POLICY_TIMESHARE ||
invalid_policy(policy))
return KERN_INVALID_ARGUMENT;
#if MACH_FIXPRI
pset_lock(pset);
/*
* Check if policy enabled. Disable if so, then handle
* change_threads.
*/
if (pset->policies & policy) {
pset->policies &= ~policy;
if (change_threads) {
queue_head_t *list;
thread_t thread;
list = &pset->threads;
queue_iterate(list, thread, thread_t, pset_threads) {
if (thread->policy == policy)
thread_policy(thread, POLICY_TIMESHARE, 0);
}
}
}
pset_unlock(pset);
#endif /* MACH_FIXPRI */
return KERN_SUCCESS;
}
#define THING_TASK 0
#define THING_THREAD 1
/*
* processor_set_things:
*
* Common internals for processor_set_{threads,tasks}
*/
kern_return_t
processor_set_things(
processor_set_t pset,
mach_port_t **thing_list,
natural_t *count,
int type)
{
unsigned int actual; /* this many things */
int i;
vm_size_t size, size_needed;
vm_offset_t addr;
if (pset == PROCESSOR_SET_NULL)
return KERN_INVALID_ARGUMENT;
size = 0; addr = 0;
for (;;) {
pset_lock(pset);
if (!pset->active) {
pset_unlock(pset);
return KERN_FAILURE;
}
if (type == THING_TASK)
actual = pset->task_count;
else
actual = pset->thread_count;
/* do we have the memory we need? */
size_needed = actual * sizeof(mach_port_t);
if (size_needed <= size)
break;
/* unlock the pset and allocate more memory */
pset_unlock(pset);
if (size != 0)
kfree(addr, size);
assert(size_needed > 0);
size = size_needed;
addr = kalloc(size);
if (addr == 0)
return KERN_RESOURCE_SHORTAGE;
}
/* OK, have memory and the processor_set is locked & active */
switch (type) {
case THING_TASK: {
task_t *tasks = (task_t *) addr;
task_t task;
for (i = 0, task = (task_t) queue_first(&pset->tasks);
i < actual;
i++, task = (task_t) queue_next(&task->pset_tasks)) {
/* take ref for convert_task_to_port */
task_reference(task);
tasks[i] = task;
}
assert(queue_end(&pset->tasks, (queue_entry_t) task));
break;
}
case THING_THREAD: {
thread_t *threads = (thread_t *) addr;
thread_t thread;
for (i = 0, thread = (thread_t) queue_first(&pset->threads);
i < actual;
i++,
thread = (thread_t) queue_next(&thread->pset_threads)) {
/* take ref for convert_thread_to_port */
thread_reference(thread);
threads[i] = thread;
}
assert(queue_end(&pset->threads, (queue_entry_t) thread));
break;
}
}
/* can unlock processor set now that we have the task/thread refs */
pset_unlock(pset);
if (actual == 0) {
/* no things, so return null pointer and deallocate memory */
*thing_list = 0;
*count = 0;
if (size != 0)
kfree(addr, size);
} else {
/* if we allocated too much, must copy */
if (size_needed < size) {
vm_offset_t newaddr;
newaddr = kalloc(size_needed);
if (newaddr == 0) {
switch (type) {
case THING_TASK: {
task_t *tasks = (task_t *) addr;
for (i = 0; i < actual; i++)
task_deallocate(tasks[i]);
break;
}
case THING_THREAD: {
thread_t *threads = (thread_t *) addr;
for (i = 0; i < actual; i++)
thread_deallocate(threads[i]);
break;
}
}
kfree(addr, size);
return KERN_RESOURCE_SHORTAGE;
}
memcpy((void *) newaddr, (void *) addr, size_needed);
kfree(addr, size);
addr = newaddr;
}
*thing_list = (mach_port_t *) addr;
*count = actual;
/* do the conversion that Mig should handle */
switch (type) {
case THING_TASK: {
task_t *tasks = (task_t *) addr;
for (i = 0; i < actual; i++)
((mach_port_t *) tasks)[i] =
(mach_port_t)convert_task_to_port(tasks[i]);
break;
}
case THING_THREAD: {
thread_t *threads = (thread_t *) addr;
for (i = 0; i < actual; i++)
((mach_port_t *) threads)[i] =
(mach_port_t)convert_thread_to_port(threads[i]);
break;
}
}
}
return KERN_SUCCESS;
}
/*
* processor_set_tasks:
*
* List all tasks in the processor set.
*/
kern_return_t
processor_set_tasks(
processor_set_t pset,
task_array_t *task_list,
natural_t *count)
{
return processor_set_things(pset, task_list, count, THING_TASK);
}
/*
* processor_set_threads:
*
* List all threads in the processor set.
*/
kern_return_t
processor_set_threads(
processor_set_t pset,
thread_array_t *thread_list,
natural_t *count)
{
return processor_set_things(pset, thread_list, count, THING_THREAD);
}
|