OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [tcpip/] [current/] [src/] [sys/] [kern/] [kern_subr.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      sys/kern/kern_subr.c
4
//
5
//     
6
//
7
//==========================================================================
8
// ####BSDALTCOPYRIGHTBEGIN####                                             
9
// -------------------------------------------                              
10
// Portions of this software may have been derived from OpenBSD             
11
// or other sources, and if so are covered by the appropriate copyright     
12
// and license included herein.                                             
13
// -------------------------------------------                              
14
// ####BSDALTCOPYRIGHTEND####                                               
15
//==========================================================================
16
//#####DESCRIPTIONBEGIN####
17
//
18
// Author(s):    gthomas
19
// Contributors: gthomas
20
// Date:         2000-01-10
21
// Purpose:      
22
// Description:  
23
//              
24
//
25
//####DESCRIPTIONEND####
26
//
27
//==========================================================================
28
 
29
 
30
/*      $OpenBSD: kern_subr.c,v 1.10 1999/11/07 17:39:14 provos Exp $   */
31
/*      $NetBSD: kern_subr.c,v 1.15 1996/04/09 17:21:56 ragge Exp $     */
32
 
33
/*
34
 * Copyright (c) 1982, 1986, 1991, 1993
35
 *      The Regents of the University of California.  All rights reserved.
36
 * (c) UNIX System Laboratories, Inc.
37
 * All or some portions of this file are derived from material licensed
38
 * to the University of California by American Telephone and Telegraph
39
 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40
 * the permission of UNIX System Laboratories, Inc.
41
 *
42
 * Redistribution and use in source and binary forms, with or without
43
 * modification, are permitted provided that the following conditions
44
 * are met:
45
 * 1. Redistributions of source code must retain the above copyright
46
 *    notice, this list of conditions and the following disclaimer.
47
 * 2. Redistributions in binary form must reproduce the above copyright
48
 *    notice, this list of conditions and the following disclaimer in the
49
 *    documentation and/or other materials provided with the distribution.
50
 * 3. All advertising materials mentioning features or use of this software
51
 *    must display the following acknowledgement:
52
 *      This product includes software developed by the University of
53
 *      California, Berkeley and its contributors.
54
 * 4. Neither the name of the University nor the names of its contributors
55
 *    may be used to endorse or promote products derived from this software
56
 *    without specific prior written permission.
57
 *
58
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68
 * SUCH DAMAGE.
69
 *
70
 *      @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
71
 */
72
 
73
#include <sys/param.h>
74
#ifndef __ECOS
75
#include <sys/systm.h>
76
#include <sys/proc.h>
77
#endif // __ECOS
78
#include <sys/malloc.h>
79
#include <sys/queue.h>
80
 
81
int
82
uiomove(cp, n, uio)
83
        register caddr_t cp;
84
        register int n;
85
        register struct uio *uio;
86
{
87
        register struct iovec *iov;
88
        u_int cnt;
89
        int error = 0;
90
 
91
#ifdef DIAGNOSTIC
92
        if (uio->uio_rw != UIO_READ && uio->uio_rw != UIO_WRITE)
93
                panic("uiomove: mode");
94
        if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
95
                panic("uiomove proc");
96
#endif
97
        while (n > 0 && uio->uio_resid) {
98
                iov = uio->uio_iov;
99
                cnt = iov->iov_len;
100
                if (cnt == 0) {
101
                        uio->uio_iov++;
102
                        uio->uio_iovcnt--;
103
                        continue;
104
                }
105
                if (cnt > n)
106
                        cnt = n;
107
                switch (uio->uio_segflg) {
108
 
109
                case UIO_USERSPACE:
110
                        if (uio->uio_rw == UIO_READ)
111
                                error = copyout(cp, iov->iov_base, cnt);
112
                        else
113
                                error = copyin(iov->iov_base, cp, cnt);
114
                        if (error)
115
                                return (error);
116
                        break;
117
 
118
                case UIO_SYSSPACE:
119
#if defined(UVM)
120
                        if (uio->uio_rw == UIO_READ)
121
                                error = kcopy(cp, iov->iov_base, cnt);
122
                        else
123
                                error = kcopy(iov->iov_base, cp, cnt);
124
                        if (error)
125
                                return(error);
126
#else
127
                        if (uio->uio_rw == UIO_READ)
128
                                bcopy((caddr_t)cp, iov->iov_base, cnt);
129
                        else
130
                                bcopy(iov->iov_base, (caddr_t)cp, cnt);
131
                        break;
132
#endif
133
                }
134
                iov->iov_base = (char *)iov->iov_base + cnt;
135
                iov->iov_len -= cnt;
136
                uio->uio_resid -= cnt;
137
                uio->uio_offset += cnt;
138
                cp += cnt;
139
                n -= cnt;
140
        }
141
        return (error);
142
}
143
 
144
#ifndef __ECOS
145
/*
146
 * Give next character to user as result of read.
147
 */
148
int
149
ureadc(c, uio)
150
        register int c;
151
        register struct uio *uio;
152
{
153
        register struct iovec *iov;
154
 
155
        if (uio->uio_resid == 0)
156
#ifdef DIAGNOSTIC
157
                panic("ureadc: zero resid");
158
#else
159
                return (EINVAL);
160
#endif
161
again:
162
        if (uio->uio_iovcnt <= 0)
163
#ifdef DIAGNOSTIC
164
                panic("ureadc: non-positive iovcnt");
165
#else
166
                return (EINVAL);
167
#endif
168
        iov = uio->uio_iov;
169
        if (iov->iov_len <= 0) {
170
                uio->uio_iovcnt--;
171
                uio->uio_iov++;
172
                goto again;
173
        }
174
        switch (uio->uio_segflg) {
175
 
176
        case UIO_USERSPACE:
177
                if (subyte(iov->iov_base, c) < 0)
178
                        return (EFAULT);
179
                break;
180
 
181
        case UIO_SYSSPACE:
182
                *(char *)iov->iov_base = c;
183
                break;
184
        }
185
        iov->iov_base++;
186
        iov->iov_len--;
187
        uio->uio_resid--;
188
        uio->uio_offset++;
189
        return (0);
190
}
191
#endif // __ECOS
192
 
193
/*
194
 * General routine to allocate a hash table.
195
 */
196
#ifdef __ECOS
197
void *
198
hashinit(int elements, int type, int flags, u_long *hashmask)
199
#else
200
void *
201
hashinit(elements, type, flags, hashmask)
202
        int elements, type, flags;
203
        u_long *hashmask;
204
#endif
205
{
206
        long hashsize;
207
        LIST_HEAD(generic, generic) *hashtbl;
208
        int i;
209
 
210
        if (elements <= 0)
211
                panic("hashinit: bad cnt");
212
        for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
213
                continue;
214
        hashsize >>= 1;
215
        hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, flags);
216
        for (i = 0; i < hashsize; i++)
217
                LIST_INIT(&hashtbl[i]);
218
        *hashmask = hashsize - 1;
219
        return (hashtbl);
220
}
221
 
222
#ifndef __ECOS
223
/*
224
 * "Shutdown hook" types, functions, and variables.
225
 */
226
 
227
struct shutdownhook_desc {
228
        LIST_ENTRY(shutdownhook_desc) sfd_list;
229
        void    (*sfd_fn) __P((void *));
230
        void    *sfd_arg;
231
};
232
 
233
LIST_HEAD(, shutdownhook_desc) shutdownhook_list;
234
 
235
int shutdownhooks_done;
236
 
237
void *
238
shutdownhook_establish(fn, arg)
239
        void (*fn) __P((void *));
240
        void *arg;
241
{
242
        struct shutdownhook_desc *ndp;
243
 
244
        ndp = (struct shutdownhook_desc *)
245
            malloc(sizeof (*ndp), M_DEVBUF, M_NOWAIT);
246
        if (ndp == NULL)
247
                return NULL;
248
 
249
        ndp->sfd_fn = fn;
250
        ndp->sfd_arg = arg;
251
        LIST_INSERT_HEAD(&shutdownhook_list, ndp, sfd_list);
252
 
253
        return (ndp);
254
}
255
 
256
void
257
shutdownhook_disestablish(vhook)
258
        void *vhook;
259
{
260
#ifdef DIAGNOSTIC
261
        struct shutdownhook_desc *dp;
262
 
263
        for (dp = shutdownhook_list.lh_first; dp != NULL;
264
            dp = dp->sfd_list.le_next)
265
                if (dp == vhook)
266
                        break;
267
        if (dp == NULL)
268
                panic("shutdownhook_disestablish: hook not established");
269
#endif
270
 
271
        LIST_REMOVE((struct shutdownhook_desc *)vhook, sfd_list);
272
}
273
 
274
/*
275
 * Run shutdown hooks.  Should be invoked immediately before the
276
 * system is halted or rebooted, i.e. after file systems unmounted,
277
 * after crash dump done, etc.
278
 */
279
void
280
doshutdownhooks()
281
{
282
        struct shutdownhook_desc *dp;
283
 
284
        if (shutdownhooks_done)
285
                return;
286
 
287
        for (dp = shutdownhook_list.lh_first; dp != NULL; dp =
288
            dp->sfd_list.le_next)
289
                (*dp->sfd_fn)(dp->sfd_arg);
290
}
291
 
292
/*
293
 * "Power hook" types, functions, and variables.
294
 */
295
 
296
struct powerhook_desc {
297
        LIST_ENTRY(powerhook_desc) sfd_list;
298
        void    (*sfd_fn) __P((int, void *));
299
        void    *sfd_arg;
300
};
301
 
302
LIST_HEAD(, powerhook_desc) powerhook_list;
303
 
304
void *
305
powerhook_establish(fn, arg)
306
        void (*fn) __P((int, void *));
307
        void *arg;
308
{
309
        struct powerhook_desc *ndp;
310
 
311
        ndp = (struct powerhook_desc *)
312
            malloc(sizeof(*ndp), M_DEVBUF, M_NOWAIT);
313
        if (ndp == NULL)
314
                return NULL;
315
 
316
        ndp->sfd_fn = fn;
317
        ndp->sfd_arg = arg;
318
        LIST_INSERT_HEAD(&powerhook_list, ndp, sfd_list);
319
 
320
        return (ndp);
321
}
322
 
323
void
324
powerhook_disestablish(vhook)
325
        void *vhook;
326
{
327
#ifdef DIAGNOSTIC
328
        struct powerhook_desc *dp;
329
 
330
        for (dp = powerhook_list.lh_first; dp != NULL;
331
            dp = dp->sfd_list.le_next)
332
                if (dp == vhook)
333
                        break;
334
        if (dp == NULL)
335
                panic("powerhook_disestablish: hook not established");
336
#endif
337
 
338
        LIST_REMOVE((struct powerhook_desc *)vhook, sfd_list);
339
        free(vhook, M_DEVBUF);
340
}
341
 
342
/*
343
 * Run power hooks.
344
 */
345
void
346
dopowerhooks(why)
347
        int why;
348
{
349
        struct powerhook_desc *dp;
350
 
351
        for (dp = LIST_FIRST(&powerhook_list);
352
             dp != NULL;
353
             dp = LIST_NEXT(dp, sfd_list)) {
354
                (*dp->sfd_fn)(why, dp->sfd_arg);
355
        }
356
}
357
#endif // __ECOS

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.