Statistics
| Branch: | Revision:

scoutos / prex-0.9.0 / sys / kern / system.c @ 03e9c04a

History | View | Annotate | Download (4.74 KB)

1
/*-
2
 * Copyright (c) 2005-2007, Kohsuke Ohtani
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 * 3. Neither the name of the author nor the names of any co-contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29

    
30
/*
31
 * system.c - system services
32
 */
33

    
34
#include <kernel.h>
35
#include <thread.h>
36
#include <sched.h>
37
#include <task.h>
38
#include <vm.h>
39
#include <irq.h>
40
#include <page.h>
41
#include <device.h>
42
#include <system.h>
43
#include <hal.h>
44
#include <sys/dbgctl.h>
45

    
46
static char        infobuf[MAXINFOSZ];        /* common information buffer */
47

    
48
static const struct kerninfo kerninfo = {
49
        "Prex",        HOSTNAME, VERSION, __DATE__, MACHINE
50
};
51

    
52
/*
53
 * Get system information.
54
 */
55
int
56
sysinfo(int type, void *buf)
57
{
58
        int error = 0;
59

    
60
        sched_lock();
61

    
62
        switch (type) {
63
        case INFO_KERNEL:
64
                memcpy(buf, &kerninfo, sizeof(kerninfo));
65
                break;
66
        case INFO_MEMORY:
67
                page_info(buf);
68
                break;
69
        case INFO_TIMER:
70
                timer_info(buf);
71
                break;
72
        case INFO_THREAD:
73
                error = thread_info(buf);
74
                break;
75
        case INFO_DEVICE:
76
                error = device_info(buf);
77
                break;
78
        case INFO_TASK:
79
                error = task_info(buf);
80
                break;
81
        case INFO_VM:
82
                error = vm_info(buf);
83
                break;
84
        case INFO_IRQ:
85
                error = irq_info(buf);
86
                break;
87
        default:
88
                error = EINVAL;
89
                break;
90
        }
91
        sched_unlock();
92
        return error;
93
}
94

    
95
/*
96
 * System call to get system information.
97
 */
98
int
99
sys_info(int type, void *buf)
100
{
101
        int error;
102
        size_t bufsz = 0;
103

    
104
        if (buf == NULL || !user_area(buf))
105
                return EFAULT;
106

    
107
        sched_lock();
108

    
109
        switch (type) {
110
        case INFO_KERNEL:
111
                bufsz = sizeof(struct kerninfo);
112
                break;
113
        case INFO_MEMORY:
114
                bufsz = sizeof(struct meminfo);
115
                break;
116
        case INFO_TIMER:
117
                bufsz = sizeof(struct timerinfo);
118
                break;
119
        case INFO_THREAD:
120
                bufsz = sizeof(struct threadinfo);
121
                break;
122
        case INFO_DEVICE:
123
                bufsz = sizeof(struct devinfo);
124
                break;
125
        case INFO_TASK:
126
                bufsz = sizeof(struct taskinfo);
127
                break;
128
        case INFO_VM:
129
                bufsz = sizeof(struct vminfo);
130
                break;
131
        case INFO_IRQ:
132
                bufsz = sizeof(struct irqinfo);
133
                break;
134
        default:
135
                sched_unlock();
136
                return EINVAL;
137
        }
138

    
139
        error = copyin(buf, &infobuf, bufsz);
140
        if (!error) {
141
                error = sysinfo(type, &infobuf);
142
                if (!error) {
143
                        error = copyout(&infobuf, buf, bufsz);
144
                }
145
        }
146
        sched_unlock();
147
        return error;
148
}
149

    
150
/*
151
 * Logging system call.
152
 *
153
 * Write a message to the logging device.  The log
154
 * function is available only when kernel is built
155
 * with debug option.
156
 */
157
int
158
sys_log(const char *str)
159
{
160
#ifdef DEBUG
161
        char buf[DBGMSGSZ];
162

    
163
        if (copyinstr(str, buf, DBGMSGSZ))
164
                return EINVAL;
165

    
166
        printf("%s", buf);
167
        return 0;
168
#else
169
        return ENOSYS;
170
#endif
171
}
172

    
173
/*
174
 * Kernel debug service.
175
 */
176
int
177
sys_debug(int cmd, void *data)
178
{
179
#ifdef DEBUG
180
        int error = EINVAL;
181
        task_t task = 0;
182

    
183
        switch (cmd) {
184
        case DBGC_LOGSIZE:
185
        case DBGC_GETLOG:
186
                error = dbgctl(cmd, data);
187
                break;
188
        case DBGC_TRACE:
189
                task = (task_t)data;
190
                if (!task_valid(task)) {
191
                        error = ESRCH;
192
                        break;
193
                }
194
                dbgctl(cmd, (void *)task);
195
                error = 0;
196
                break;
197
        }
198
        return error;
199
#else
200
        return ENOSYS;
201
#endif
202
}
203

    
204
/*
205
 * Panic system call.
206
 */
207
int
208
sys_panic(const char *str)
209
{
210
#ifdef DEBUG
211
        char buf[DBGMSGSZ];
212

    
213
        sched_lock();
214
        copyinstr(str, buf, DBGMSGSZ - 20);
215
        printf("User panic: %s\n", str);
216
        printf(" task=%s thread=%lx\n", curtask->name, (long)curthread);
217

    
218
        machine_abort();
219
        /* NOTREACHED */
220
#else
221
        task_terminate(curtask);
222
#endif
223
        return 0;
224
}
225

    
226
/*
227
 * Get system time - return ticks since OS boot.
228
 */
229
int
230
sys_time(u_long *ticks)
231
{
232
        u_long t;
233

    
234
        t = timer_ticks();
235
        return copyout(&t, ticks, sizeof(t));
236
}
237

    
238
/*
239
 * nonexistent system call.
240
 */
241
int
242
sys_nosys(void)
243
{
244
        return EINVAL;
245
}