Oops, forgot this file last night. New error codes
[newos.git] / kernel / cpu.c
blobdd8035d68fd03d8374a4557a9ef624f554d5cfca
1 /*
2 ** Copyright 2002-2004, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
5 #include <kernel/kernel.h>
6 #include <kernel/cpu.h>
7 #include <kernel/vm.h>
8 #include <kernel/time.h>
9 #include <boot/stage2.h>
11 #include <string.h>
13 /* global per-cpu structure */
14 cpu_ent cpu[_MAX_CPUS];
16 int cpu_init(kernel_args *ka)
18 int i;
20 memset(cpu, 0, sizeof(cpu));
21 for(i = 0; i < _MAX_CPUS; i++) {
22 cpu[i].cpu_num = i;
25 return arch_cpu_init(ka);
28 int cpu_init_percpu(kernel_args *ka, int curr_cpu)
30 return arch_cpu_init_percpu(ka, curr_cpu);
33 int cpu_preboot_init(kernel_args *ka)
35 return arch_cpu_preboot_init(ka);
38 void cpu_spin(bigtime_t interval)
40 bigtime_t start = system_time();
42 while(system_time() - start < interval)
46 int user_atomic_add(int *uval, int incr)
48 int val;
49 int ret;
51 if(is_kernel_address(uval))
52 goto error;
54 if(user_memcpy(&val, uval, sizeof(val)) < 0)
55 goto error;
57 ret = val;
58 val += incr;
60 if(user_memcpy(uval, &val, sizeof(val)) < 0)
61 goto error;
63 return ret;
65 error:
66 // XXX kill the app
67 return -1;
70 int user_atomic_and(int *uval, int incr)
72 int val;
73 int ret;
75 if(is_kernel_address(uval))
76 goto error;
78 if(user_memcpy(&val, uval, sizeof(val)) < 0)
79 goto error;
81 ret = val;
82 val &= incr;
84 if(user_memcpy(uval, &val, sizeof(val)) < 0)
85 goto error;
87 return ret;
89 error:
90 // XXX kill the app
91 return -1;
94 int user_atomic_or(int *uval, int incr)
96 int val;
97 int ret;
99 if(is_kernel_address(uval))
100 goto error;
102 if(user_memcpy(&val, uval, sizeof(val)) < 0)
103 goto error;
105 ret = val;
106 val |= incr;
108 if(user_memcpy(uval, &val, sizeof(val)) < 0)
109 goto error;
111 return ret;
113 error:
114 // XXX kill the app
115 return -1;
118 int user_atomic_set(int *uval, int set_to)
120 int val;
121 int ret;
123 if(is_kernel_address(uval))
124 goto error;
126 if(user_memcpy(&val, uval, sizeof(val)) < 0)
127 goto error;
129 ret = val;
130 val = set_to;
132 if(user_memcpy(uval, &val, sizeof(val)) < 0)
133 goto error;
135 return ret;
137 error:
138 // XXX kill the app
139 return -1;
142 int user_test_and_set(int *uval, int set_to, int test_val)
144 int val;
145 int ret;
147 if(is_kernel_address(uval))
148 goto error;
150 if(user_memcpy(&val, uval, sizeof(val)) < 0)
151 goto error;
153 ret = val;
154 if(val == test_val) {
155 val = set_to;
156 if(user_memcpy(uval, &val, sizeof(val)) < 0)
157 goto error;
160 return ret;
162 error:
163 // XXX kill the app
164 return -1;