]>
Commit | Line | Data |
---|---|---|
813fb2f6 A |
1 | #include <pthread.h> |
2 | #include <stdlib.h> | |
3 | #include <stdio.h> | |
4 | #include <unistd.h> | |
5 | ||
6 | #include <mach/mach.h> | |
7 | #include <mach/mach_vm.h> | |
8 | ||
9 | static void* loop(__attribute__ ((unused)) void *arg) { | |
10 | while (1) { | |
11 | ||
12 | } | |
13 | } | |
14 | ||
15 | ||
16 | static int run_additional_threads(int nthreads) { | |
17 | for (int i = 0; i < nthreads; i++) { | |
18 | pthread_t pthread; | |
19 | int err; | |
20 | ||
21 | err = pthread_create(&pthread, NULL, loop, NULL); | |
22 | if (err) { | |
23 | return err; | |
24 | } | |
25 | } | |
26 | ||
27 | return 0; | |
28 | } | |
29 | ||
30 | static int allocate_and_wire_memory(mach_vm_size_t size) { | |
31 | int err; | |
32 | task_t task = mach_task_self(); | |
33 | mach_vm_address_t addr; | |
34 | ||
35 | if (size <= 0) | |
36 | return 0; | |
37 | ||
38 | err = mach_vm_allocate(task, &addr, size, VM_FLAGS_ANYWHERE); | |
39 | if (err != KERN_SUCCESS) { | |
40 | printf("mach_vm_allocate returned non-zero: %s\n", mach_error_string(err)); | |
41 | return err; | |
42 | } | |
43 | err = mach_vm_protect(task, addr, size, 0, VM_PROT_READ | VM_PROT_WRITE);; | |
44 | if (err != KERN_SUCCESS) { | |
45 | printf("mach_vm_protect returned non-zero: %s\n", mach_error_string(err)); | |
46 | return err; | |
47 | } | |
48 | host_t host_priv_port; | |
49 | err = host_get_host_priv_port(mach_host_self(), &host_priv_port); | |
50 | if (err != KERN_SUCCESS) { | |
51 | printf("host_get_host_priv_port retruned non-zero: %s\n", mach_error_string(err)); | |
52 | return err; | |
53 | } | |
54 | err = mach_vm_wire(host_priv_port, task, addr, size, VM_PROT_READ | VM_PROT_WRITE); | |
55 | if (err != KERN_SUCCESS) { | |
56 | printf("mach_vm_wire returned non-zero: %s\n", mach_error_string(err)); | |
57 | return err; | |
58 | } | |
59 | ||
60 | return 0; | |
61 | } | |
62 | ||
813fb2f6 | 63 | int main(int argc, char *argv[]) { |
d9a64523 | 64 | int nthreads = 0; |
813fb2f6 A |
65 | int err; |
66 | mach_vm_size_t wired_mem = 0; | |
67 | ||
68 | if (argc > 1) { | |
d9a64523 | 69 | nthreads = (int)strtoul(argv[1], NULL, 10); |
813fb2f6 A |
70 | } |
71 | if (argc > 2) { | |
d9a64523 | 72 | wired_mem = (mach_vm_size_t)strtoul(argv[2], NULL, 10); |
813fb2f6 A |
73 | } |
74 | ||
75 | err = allocate_and_wire_memory(wired_mem); | |
76 | if (err) { | |
77 | return err; | |
78 | } | |
79 | ||
813fb2f6 A |
80 | err = run_additional_threads(nthreads); |
81 | if (err) { | |
82 | return err; | |
83 | } | |
84 | ||
85 | return 0; | |
86 | } |