Annotation of qemu/roms/SLOF/clients/net-snk/kernel/systemcall.c, revision 1.1.1.2

1.1       root        1: /******************************************************************************
                      2:  * Copyright (c) 2004, 2008 IBM Corporation
                      3:  * All rights reserved.
                      4:  * This program and the accompanying materials
                      5:  * are made available under the terms of the BSD License
                      6:  * which accompanies this distribution, and is available at
                      7:  * http://www.opensource.org/licenses/bsd-license.php
                      8:  *
                      9:  * Contributors:
                     10:  *     IBM Corporation - initial implementation
                     11:  *****************************************************************************/
                     12: 
1.1.1.2 ! root       13: #include <stdint.h>
        !            14: #include <stdarg.h>
        !            15: #include <string.h>
1.1       root       16: #include <of.h>
                     17: #include <systemcall.h>
                     18: #include <netdriver_int.h>
                     19: #include <fileio.h>
1.1.1.2 ! root       20: #include "modules.h"
1.1       root       21: 
                     22: extern int vsprintf(char *, const char *, va_list);
                     23: extern void _exit(int status);
                     24: 
1.1.1.2 ! root       25: long _system_call(long arg0, long arg1, long arg2, long arg3, 
        !            26:                  long arg4, long arg5, long arg6, int nr);
        !            27: void exit(int status);
1.1       root       28: int printk(const char*, ...);
                     29: 
                     30: static int
                     31: _syscall_open(const char* name, int flags)
                     32: {
                     33:        int fd, i;
                     34: 
                     35:        /* search free file descriptor */
                     36:        for(fd=0; fd<FILEIO_MAX; ++fd) {
                     37:                if(fd_array[fd].type == FILEIO_TYPE_EMPTY) {
                     38:                        break;
                     39:                }
                     40:        }
                     41:        if(fd == FILEIO_MAX) {
                     42:                printk ("Can not open \"%s\" because file descriptor list is full\n", name);
                     43:                /* there is no free file descriptor avaliable */
                     44:                return -2;
                     45:        }
                     46: 
                     47:        for(i=0; i<MODULES_MAX; ++i) {
                     48:                if(!snk_modules[i] || !snk_modules[i]->open) {
                     49:                        continue;
                     50:                }
                     51: 
                     52:                if(snk_modules[i]->running == 0) {
                     53:                        snk_modules[i]->init();
                     54:                }
                     55: 
                     56:                if(snk_modules[i]->open(&fd_array[fd], name, flags) == 0)
                     57:                        break;
                     58:        }
                     59: 
                     60:        if(i==MODULES_MAX) {
                     61:                /* file not found */
                     62:                return -1;
                     63:        }
                     64: 
                     65:        return fd;
                     66: }
                     67: 
                     68: static int
                     69: _syscall_socket(int domain, int type, int proto, char *mac_addr)
                     70: {
                     71:        snk_module_t *net_module;
                     72: 
                     73:        net_module = get_module_by_type(MOD_TYPE_NETWORK);
                     74:        if( !net_module ||  !net_module->init) {
                     75:                printk("No net_init function available");
                     76:                return -1;
                     77:        }
                     78: 
                     79:        /* Init net device driver */
                     80:        if(net_module->running == 0) {
                     81:                net_module->init();
                     82:        }
                     83: 
                     84:        if(net_module->running == 0)
                     85:                return -2;
                     86: 
                     87:        memcpy(mac_addr, &net_module->mac_addr[0], 6);
                     88:        return 0;
                     89: }
                     90: 
                     91: static int
                     92: _syscall_close(int fd)
                     93: {
                     94:        if(fd < 0 || fd >= FILEIO_MAX
                     95:        || fd_array[fd].type == FILEIO_TYPE_EMPTY
                     96:        || fd_array[fd].close == 0)
                     97:                return -1;
                     98: 
                     99:        return fd_array[fd].close(&fd_array[fd]);
                    100: }
                    101: 
                    102: static long
                    103: _syscall_read (int fd, char *buf, long len)
                    104: {
                    105:        if(fd < 0 || fd >= FILEIO_MAX
                    106:        || fd_array[fd].type == FILEIO_TYPE_EMPTY
                    107:        || fd_array[fd].read == 0)
                    108:                return -1;
                    109: 
                    110:        return fd_array[fd].read(&fd_array[fd], buf, len);
                    111: }
                    112: 
                    113: static long
                    114: _syscall_write (int fd, char *buf, long len)
                    115: {
                    116:     char dest_buf[512];
                    117:     char *dest_buf_ptr;
                    118:     int i;
                    119:     if (fd == 1 || fd == 2)
                    120:     {
                    121:        dest_buf_ptr = &dest_buf[0];
                    122:         for (i = 0; i < len && i < 256; i++)
                    123:         {
                    124:             *dest_buf_ptr++ = *buf++;
                    125:             if (buf[-1] == '\n')
                    126:                 *dest_buf_ptr++ = '\r';
                    127:        }
                    128:        len = dest_buf_ptr - &dest_buf[0];
                    129:        buf = &dest_buf[0];
                    130:     }
                    131: 
                    132:        if(fd < 0 || fd >= FILEIO_MAX
                    133:        || fd_array[fd].type == FILEIO_TYPE_EMPTY
                    134:        || fd_array[fd].write == 0)
                    135:                return -1;
                    136: 
                    137:        return fd_array[fd].write(&fd_array[fd], buf, len);
                    138: }
                    139: 
                    140: static long
                    141: _syscall_lseek (int fd, long offset, int whence)
                    142: {
                    143:        return 0; // this syscall is unused !!!
                    144: #if 0
                    145:     if (whence != 0)
                    146:        return -1;
                    147: 
                    148:     of_seek (fd_array[fd], (unsigned int) (offset>>32), (unsigned int) (offset & 0xffffffffULL));
                    149: 
                    150:     return offset;
                    151: #endif
                    152: }
                    153: 
                    154: static int
                    155: _syscall_ioctl (int fd, int request, void* data)
                    156: {
                    157:        if (fd < 0
                    158:         || fd >= FILEIO_MAX
                    159:         || fd_array[fd].type == FILEIO_TYPE_EMPTY)
                    160:                return -1;
                    161:        if (!fd_array[fd].ioctl) { /* for backwards compatibility with network modules */
                    162:                snk_module_t *net_module;
                    163: 
                    164:                net_module = get_module_by_type(MOD_TYPE_NETWORK);
                    165:                if ( !net_module || !net_module->ioctl ) {
                    166:                        printk("No net_ioctl function available");
                    167:                        return -1;
                    168:                }
                    169: 
                    170:                return net_module->ioctl(request, data);
                    171:        }
                    172: 
                    173:        return fd_array[fd].ioctl(&fd_array[fd], request, data);
                    174: }
                    175: 
                    176: static long
                    177: _syscall_recv(int fd, void *packet, int packet_len, int flags)
                    178: {
                    179:        snk_module_t *net_module;
                    180: 
                    181:        net_module = get_module_by_type(MOD_TYPE_NETWORK);
                    182:        if( !net_module || !net_module->read ) {
                    183:                printk("No net_receive function available");
                    184:                return -1;
                    185:        }
                    186: 
                    187:        return net_module->read(packet, packet_len);
                    188: }
                    189: 
                    190: static long
                    191: _syscall_send(int fd, void *packet, int packet_len, int flags)
                    192: {
                    193:        snk_module_t *net_module;
                    194: 
                    195:        net_module = get_module_by_type(MOD_TYPE_NETWORK);
                    196:        if( !net_module || !net_module->write ) {
                    197:                printk("No net_xmit function available");
                    198:                return -1;
                    199:        }
                    200: 
                    201:        return net_module->write(packet, packet_len);
                    202: }
                    203: 
                    204: static long
                    205: _syscall_sendto(int fd, void *packet, int packet_len, int flags,
                    206:        void *sock_addr, int sock_addr_len)
                    207: {
                    208:        return _syscall_send(fd, packet, packet_len, flags);
                    209: }
                    210: 
                    211: 
                    212: 
                    213: 
                    214: 
                    215: 
                    216: 
                    217: 
                    218: long
                    219: _system_call(long arg0, long arg1, long arg2, long arg3, 
                    220:             long arg4, long arg5, long arg6, int nr)
                    221: {
                    222:        long rc = -1;
                    223: 
                    224:        switch (nr)
                    225:        {
                    226:        case _open_sc_nr:
                    227:                rc = _syscall_open ((void *) arg0, arg1);
                    228:                break;
                    229:        case _read_sc_nr:
                    230:                rc = _syscall_read (arg0, (void *) arg1, arg2);
                    231:                break;
                    232:        case _close_sc_nr:
                    233:                _syscall_close (arg0);
                    234:                break;
                    235:        case _lseek_sc_nr:
                    236:                rc = _syscall_lseek (arg0, arg1, arg2);
                    237:                break;
                    238:        case _write_sc_nr:
                    239:                rc = _syscall_write (arg0, (void *) arg1, arg2);
                    240:                break;
                    241:        case _ioctl_sc_nr:
                    242:                rc = _syscall_ioctl (arg0, arg1, (void *) arg2);
                    243:                break;
                    244:        case _socket_sc_nr:
                    245:                switch (arg0)
                    246:                {
                    247:                case _sock_sc_nr:
                    248:                        rc = _syscall_socket (arg1, arg2, arg3, (char*) arg4);
                    249:                        break;
                    250:                case _recv_sc_nr:
                    251:                        rc = _syscall_recv (arg1, (void *) arg2, arg3, arg4);
                    252:                        break;
                    253:                case _send_sc_nr:
                    254:                        rc = _syscall_send (arg1, (void *) arg2, arg3, arg4);
                    255:                        break;
                    256:                case _sendto_sc_nr:
                    257:                        rc = _syscall_sendto (arg1, (void *) arg2, arg3, arg4, (void *) arg5, arg6);
                    258:                        break;
                    259:                default:
                    260:                        break;
                    261:                }
                    262:                break;
                    263:        default:
                    264:                break;
                    265:        }
                    266: 
                    267:        return rc;
                    268: }
                    269: 
                    270: void 
                    271: exit(int status)
                    272: {
                    273:        _exit(status);
                    274: }
                    275: 
                    276: int
                    277: printk(const char* fmt, ...)
                    278: {
                    279:        int count;
                    280:        va_list ap;
                    281:        char buffer[256];
                    282:        va_start (ap, fmt);
                    283:        count=vsprintf(buffer, fmt, ap);
                    284:        _syscall_write (1, buffer, count);
                    285:        va_end (ap);
                    286:        return count;
                    287: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.