source: soft/giet_vm/sys/sys_handler.c @ 228

Last change on this file since 228 was 228, checked in by meunier, 11 years ago

Added support for memspaces and const.
Added an interrupt masking to the "giet_context_switch" syscall
Corrected two bugs in boot/boot_init.c (one minor and one regarding barriers initialization)
Reformatted the code in all files.

File size: 8.2 KB
Line 
1///////////////////////////////////////////////////////////////////////////////////
2// File     : sys_handler.c
3// Date     : 01/04/2012
4// Author   : alain greiner and joel porquet
5// Copyright (c) UPMC-LIP6
6///////////////////////////////////////////////////////////////////////////////////
7// The sys_handler.c and sys_handler.h files are part of the GIET-VM nano-kernel.
8// It define the syscall_vector[] (at the end of this file), as well as the
9// associated syscall handlers that are not related to peripherals.
10// The syscall handlers for peripherals are defined in the drivers.c file.
11///////////////////////////////////////////////////////////////////////////////////
12
13#include <sys_handler.h>
14#include <drivers.h>
15#include <ctx_handler.h>
16#include <common.h>
17#include <giet_config.h>
18#include <mapping_info.h>
19#include <srl_memspace.h>
20
21////////////////////////////////////////////////////////////////////////////
22//    Initialize the syscall vector with syscall handlers
23////////////////////////////////////////////////////////////////////////////
24const void * _syscall_vector[32] = {
25    &_procid,              /* 0x00 */
26    &_proctime,            /* 0x01 */
27    &_tty_write,           /* 0x02 */
28    &_tty_read,            /* 0x03 */
29    &_timer_start,         /* 0x04 */
30    &_timer_stop,          /* 0x05 */
31    &_gcd_write,           /* 0x06 */
32    &_gcd_read,            /* 0x07 */
33    &_sys_ukn,             /* 0x08 */
34    &_get_current_task_id, /* 0x09 */
35    &_sys_ukn,             /* 0x0A */
36    &_sys_ukn,             /* 0x0B */
37    &_sys_ukn,             /* 0x0C */
38    &_context_switch,      /* 0x0D */
39    &_exit,                /* 0x0E */
40    &_procs_number,        /* 0x0F */
41    &_fb_sync_write,       /* 0x10 */
42    &_fb_sync_read,        /* 0x11 */
43    &_fb_write,            /* 0x12 */
44    &_fb_read,             /* 0x13 */
45    &_fb_completed,        /* 0x14 */
46    &_ioc_write,           /* 0x15 */
47    &_ioc_read,            /* 0x16 */
48    &_ioc_completed,       /* 0x17 */
49    &_sys_ukn,             /* 0x18 */
50    &_sys_ukn,             /* 0x19 */
51    &_vobj_get_vbase,      /* 0x1A */
52    &_nic_write,           /* 0x1B */
53    &_nic_read,            /* 0x1C */
54    &_nic_completed,       /* 0x1D */
55    &_sys_ukn,             /* 0x1E */
56    &_sys_ukn,             /* 0x1F */
57};
58
59//////////////////////////////////////////////////////////////////////////////
60// function executed in case of undefined syscall
61//////////////////////////////////////////////////////////////////////////////
62void _sys_ukn() {
63    unsigned int epc;
64    asm volatile("mfc0 %0, $14" : "=r" (epc));
65
66    _puts("\n\n!!! Undefined System Call !!!\n");
67    _puts("\nEPC = ");
68    _putx(epc);
69    _exit();
70}
71
72
73////////////////////////////////////////////////////////////////////////////
74// _exit()
75// Task suicide... after printing a death message.
76////////////////////////////////////////////////////////////////////////////
77void _exit() {
78    unsigned int date = _proctime();
79    unsigned int proc_id = _procid();
80    unsigned int task_id = _get_current_task_id();
81
82    // print death message
83    _get_lock(&_tty_put_lock);
84    _puts("\n[GIET] Exit task ");
85    _putd(task_id);
86    _puts(" on processor ");
87    _putd(proc_id);
88    _puts(" at cycle ");
89    _putd(date);
90    _puts("\n\n");
91    _release_lock(&_tty_put_lock);
92
93    // goes to sleeping state
94    _set_context_slot( task_id, CTX_RUN_ID, 0);
95
96    // deschedule
97    _ctx_switch();
98} 
99
100
101//////////////////////////////////////////////////////////////////////////////
102// _procid()
103// Access CP0 and returns current processor's identifier.
104// Max number or processors is 1024.
105//////////////////////////////////////////////////////////////////////////////
106unsigned int _procid() {
107    unsigned int ret;
108    asm volatile("mfc0 %0, $15, 1" : "=r" (ret));
109    return (ret & 0xFFF);
110}
111
112
113//////////////////////////////////////////////////////////////////////////////
114// _proctime()
115// Access CP0 and returns current processor's elapsed clock cycles since boot.
116//////////////////////////////////////////////////////////////////////////////
117unsigned int _proctime() {
118    unsigned int ret;
119    asm volatile("mfc0 %0, $9" : "=r" (ret));
120    return ret;
121}
122
123
124//////////////////////////////////////////////////////////////////////////////
125// _procnumber()
126// returns in buffer argument the number of processors in the cluster
127// specified by the cluster_id argument.
128//////////////////////////////////////////////////////////////////////////////
129unsigned int _procs_number(unsigned int cluster_id, unsigned int * buffer) {
130    mapping_header_t * header  = (mapping_header_t *) &seg_mapping_base;
131    mapping_cluster_t * cluster = _get_cluster_base(header);
132
133    if (cluster_id < header->clusters) {
134        *buffer = cluster[cluster_id].procs;
135        return 0;
136    }
137    else {
138        return 1;
139    }
140}
141
142
143int _get_vobj(char * vspace_name, char * vobj_name, unsigned int vobj_type, mapping_vobj_t ** res_vobj) {
144    mapping_header_t * header = (mapping_header_t *) &seg_mapping_base;
145    mapping_vspace_t * vspace = _get_vspace_base(header);
146    mapping_vobj_t * vobj  = _get_vobj_base(header);
147
148    unsigned int vspace_id;
149    unsigned int vobj_id;
150
151
152    // scan vspaces
153    for (vspace_id = 0; vspace_id < header->vspaces; vspace_id++) {
154        if (_strncmp( vspace[vspace_id].name, vspace_name, 31) == 0) {
155            // scan vobjs
156            for (vobj_id = vspace[vspace_id].vobj_offset; 
157                 vobj_id < (vspace[vspace_id].vobj_offset + vspace[vspace_id].vobjs); 
158                 vobj_id++) {
159
160                if (_strncmp(vobj[vobj_id].name, vobj_name, 31) == 0) {
161                    if (vobj[vobj_id].type != vobj_type) {
162                        _get_lock(&_tty_put_lock);
163                        _puts("*** Error in _get_obj: wrong type\n");
164                        _release_lock(&_tty_put_lock);
165                        return -1; //wrong type
166                    }
167                    *res_vobj = &vobj[vobj_id];
168                    return 0;
169                }
170            } 
171        }
172    } 
173    _get_lock(&_tty_put_lock);
174    _puts("*** Error in _get_obj: object not found\n");
175    _release_lock(&_tty_put_lock);
176
177    return -2; //not found
178}
179
180
181/////////////////////////////////////////////////////////////////////////////
182// _vobj_get_vbase()
183// This function writes in vobj_vaddr the virtual base address of a vobj
184// identified by the (vspace_name / vobj_name ) couple.
185// The vobj_type argument is here only for the purpose of checking .
186// returns 0: success, else: failed.
187/////////////////////////////////////////////////////////////////////////////
188unsigned int _vobj_get_vbase(
189        char * vspace_name,
190        char * vobj_name,
191        unsigned int vobj_type,
192        unsigned int * vobj_vaddr) {
193    mapping_vobj_t * res_vobj;
194    unsigned int ret;
195    if ((ret = _get_vobj(vspace_name, vobj_name, vobj_type, &res_vobj))) {
196        return ret;
197    }
198
199    *vobj_vaddr = res_vobj->vaddr;
200    return 0;
201}
202
203
204/////////////////////////////////////////////////////////////////////////////
205// _vobj_get_length()
206// This function writes in vobj_length the virtual base address of a vobj
207// identified by the (vspace_name / vobj_name ) couple.
208// The vobj_type argument is here only for the purpose of checking .
209// returns 0: success, else: failed.
210/////////////////////////////////////////////////////////////////////////////
211unsigned int _vobj_get_length(
212        char * vspace_name, 
213        char * vobj_name,
214        unsigned int vobj_type, 
215        unsigned int * vobj_length) {
216
217    mapping_vobj_t * res_vobj;
218    unsigned int ret;
219    if ((ret = _get_vobj(vspace_name, vobj_name, vobj_type, &res_vobj))) {
220        return ret;
221    }
222
223    *vobj_length = res_vobj->length;
224
225    return 0;
226}
227
228
229////////////////////////////////////////////////////////////////
230// _context_switch()
231// This functions masks interruptions before calling _ctx_switch
232// (They are usually masked when we receive a isr_switch interrupt
233// because we execute isrs with interrupt masked)
234////////////////////////////////////////////////////////////////
235void _context_switch() {
236    _it_mask();
237    _ctx_switch();
238    _it_restore();
239}
240
241
242// Local Variables:
243// tab-width: 4
244// c-basic-offset: 4
245// c-file-offsets:((innamespace . 0)(inline-open . 0))
246// indent-tabs-mode: nil
247// End:
248// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
249
Note: See TracBrowser for help on using the repository browser.