[BACK]Return to system.c CVS log [TXT][DIR] Up to [local] / prex-old / sys / kern

Annotation of prex-old/sys/kern/system.c, Revision 1.1

1.1     ! nbrk        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 <irq.h>
        !            39: #include <page.h>
        !            40: #include <kmem.h>
        !            41: #include <vm.h>
        !            42: #include <device.h>
        !            43: #include <system.h>
        !            44:
        !            45: /*
        !            46:  * kernel information.
        !            47:  */
        !            48: static const struct info_kernel kern_info = KERNEL_INFO(kern_info);
        !            49:
        !            50: /*
        !            51:  * Logging system call.
        !            52:  *
        !            53:  * Write a message to the logging device.
        !            54:  * The log function is available only when kernel is built with
        !            55:  * debug option.
        !            56:  */
        !            57: int
        !            58: sys_log(const char *str)
        !            59: {
        !            60: #ifdef DEBUG
        !            61:        char buf[MSGBUFSZ];
        !            62:        size_t len;
        !            63:
        !            64:        if (umem_strnlen(str, MSGBUFSZ, &len))
        !            65:                return EFAULT;
        !            66:        if (len >= MSGBUFSZ)
        !            67:                return EINVAL;
        !            68:        if (umem_copyin((void *)str, buf, len + 1))
        !            69:                return EFAULT;
        !            70:        printk(buf);
        !            71:        return 0;
        !            72: #else
        !            73:        return ENOSYS;
        !            74: #endif
        !            75: }
        !            76:
        !            77: /*
        !            78:  * Panic system call.
        !            79:  *
        !            80:  * Kernel behavior for sys_panic() is different for its debug option.
        !            81:  *  - Debug build
        !            82:  *     Show a panic message and stop the entire system.
        !            83:  *  - Release build
        !            84:  *     Terminate the task which called sys_panic().
        !            85:  */
        !            86: int
        !            87: sys_panic(const char *str)
        !            88: {
        !            89: #ifdef DEBUG
        !            90:        irq_lock();
        !            91:        printk("\nUser mode panic: task:%s thread:%x\n",
        !            92:               cur_task()->name ? cur_task()->name : "no name", cur_thread);
        !            93:
        !            94:        sys_log(str);
        !            95:        printk("\n");
        !            96:
        !            97:        sched_lock();
        !            98:        irq_unlock();
        !            99:        BREAKPOINT();
        !           100:
        !           101:        for (;;);
        !           102: #else
        !           103:        task_terminate(cur_task());
        !           104: #endif
        !           105:        /* NOTREACHED */
        !           106:        return 0;
        !           107: }
        !           108:
        !           109: /*
        !           110:  * Get system information
        !           111:  */
        !           112: int
        !           113: sys_info(int type, void *buf)
        !           114: {
        !           115:        int err = 0;
        !           116:        struct info_memory imem;
        !           117:        struct info_timer itmr;
        !           118:        struct info_thread ithr;
        !           119:        struct info_device idev;
        !           120:
        !           121:        if (buf == NULL || !user_area(buf))
        !           122:                return EFAULT;
        !           123:
        !           124:        switch (type) {
        !           125:        case INFO_KERNEL:
        !           126:                err = umem_copyout((void *)&kern_info, buf,
        !           127:                                   sizeof(kern_info));
        !           128:                break;
        !           129:
        !           130:        case INFO_MEMORY:
        !           131:                page_info(&imem.total, &imem.free);
        !           132:                kmem_info(&imem.kernel);
        !           133:                err = umem_copyout(&imem, buf, sizeof(imem));
        !           134:                break;
        !           135:
        !           136:        case INFO_THREAD:
        !           137:                if (umem_copyin(buf, &ithr, sizeof(ithr)))
        !           138:                        return EFAULT;
        !           139:                if ((err = thread_info(&ithr)))
        !           140:                        return err;
        !           141:                ithr.cookie++;
        !           142:                err = umem_copyout(&ithr, buf, sizeof(ithr));
        !           143:                break;
        !           144:
        !           145:        case INFO_DEVICE:
        !           146:                if (umem_copyin(buf, &idev, sizeof(idev)))
        !           147:                        return EFAULT;
        !           148:                if ((err = device_info(&idev)))
        !           149:                        return err;
        !           150:                idev.cookie++;
        !           151:                err = umem_copyout(&idev, buf, sizeof(idev));
        !           152:                break;
        !           153:
        !           154:        case INFO_TIMER:
        !           155:                timer_info(&itmr);
        !           156:                err = umem_copyout(&itmr, buf, sizeof(itmr));
        !           157:                break;
        !           158:
        !           159:        default:
        !           160:                err = EINVAL;
        !           161:        }
        !           162:        return err;
        !           163: }
        !           164:
        !           165: /*
        !           166:  * Get system time - return ticks from OS boot.
        !           167:  */
        !           168: int
        !           169: sys_time(u_long *ticks)
        !           170: {
        !           171:        u_long t;
        !           172:
        !           173:        t = timer_count();
        !           174:        return umem_copyout(&t, ticks, sizeof(u_long));
        !           175: }
        !           176:
        !           177: /*
        !           178:  * Kernel debug service.
        !           179:  */
        !           180: int
        !           181: sys_debug(int cmd, u_long param)
        !           182: {
        !           183: #ifdef DEBUG
        !           184:        int err = EINVAL;
        !           185:        size_t size;
        !           186:        char *buf;
        !           187:
        !           188:        /*
        !           189:         * Check capability for some commands.
        !           190:         */
        !           191:        switch (cmd) {
        !           192:        case DCMD_DUMP:
        !           193:                if (!task_capable(CAP_DEBUG))
        !           194:                        return EPERM;
        !           195:        }
        !           196:
        !           197:        switch (cmd) {
        !           198:        case DCMD_DUMP:
        !           199:                err = debug_dump(param);
        !           200:                break;
        !           201:        case DCMD_LOGSIZE:
        !           202:                if ((err = log_get(&buf, &size)) == 0)
        !           203:                        err = umem_copyout(&size, (void *)param, sizeof(size));
        !           204:                break;
        !           205:        case DCMD_GETLOG:
        !           206:                if ((err = log_get(&buf, &size)) == 0)
        !           207:                        err = umem_copyout(buf, (void *)param, size);
        !           208:                break;
        !           209:        default:
        !           210:                break;
        !           211:        }
        !           212:        return err;
        !           213: #else
        !           214:        return ENOSYS;
        !           215: #endif
        !           216: }

CVSweb