Subversion Repositories HelenOS-historic

Rev

Rev 1100 | Rev 1702 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1100 Rev 1264
1
/*
1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
-
 
29
/**
-
 
30
 * @file    func.c
-
 
31
 * @brief   Miscellaneous functions.
-
 
32
 */
-
 
33
 
29
#include <func.h>
34
#include <func.h>
30
#include <print.h>
35
#include <print.h>
31
#include <cpu.h>
36
#include <cpu.h>
32
#include <arch/asm.h>
37
#include <arch/asm.h>
33
#include <arch.h>
38
#include <arch.h>
34
#include <typedefs.h>
39
#include <typedefs.h>
35
#include <console/kconsole.h>
40
#include <console/kconsole.h>
36
 
41
 
37
atomic_t haltstate = {0}; /**< Halt flag */
42
atomic_t haltstate = {0}; /**< Halt flag */
38
 
43
 
39
 
44
 
40
/** Halt wrapper
45
/** Halt wrapper
41
 *
46
 *
42
 * Set halt flag and halt the cpu.
47
 * Set halt flag and halt the cpu.
43
 *
48
 *
44
 */
49
 */
45
void halt()
50
void halt()
46
{
51
{
47
#ifdef CONFIG_DEBUG
52
#ifdef CONFIG_DEBUG
48
    bool rundebugger = false;
53
    bool rundebugger = false;
49
 
54
 
50
//      TODO test_and_set not defined on all arches
55
//      TODO test_and_set not defined on all arches
51
//  if (!test_and_set(&haltstate))
56
//  if (!test_and_set(&haltstate))
52
    if (!atomic_get(&haltstate)) {
57
    if (!atomic_get(&haltstate)) {
53
        atomic_set(&haltstate, 1);
58
        atomic_set(&haltstate, 1);
54
        rundebugger = true;
59
        rundebugger = true;
55
    }
60
    }
56
#else
61
#else
57
    atomic_set(&haltstate, 1);
62
    atomic_set(&haltstate, 1);
58
#endif
63
#endif
59
 
64
 
60
    interrupts_disable();
65
    interrupts_disable();
61
#ifdef CONFIG_DEBUG
66
#ifdef CONFIG_DEBUG
62
    if (rundebugger) {
67
    if (rundebugger) {
63
        printf("\n");
68
        printf("\n");
64
        kconsole("panic"); /* Run kconsole as a last resort to user */
69
        kconsole("panic"); /* Run kconsole as a last resort to user */
65
    }
70
    }
66
#endif      
71
#endif      
67
    if (CPU)
72
    if (CPU)
68
        printf("cpu%d: halted\n", CPU->id);
73
        printf("cpu%d: halted\n", CPU->id);
69
    else
74
    else
70
        printf("cpu: halted\n");
75
        printf("cpu: halted\n");
71
    cpu_halt();
76
    cpu_halt();
72
}
77
}
73
 
78
 
74
/** Return number of characters in a string.
79
/** Return number of characters in a string.
75
 *
80
 *
76
 * @param str NULL terminated string.
81
 * @param str NULL terminated string.
77
 *
82
 *
78
 * @return Number of characters in str.
83
 * @return Number of characters in str.
79
 */
84
 */
80
size_t strlen(const char *str)
85
size_t strlen(const char *str)
81
{
86
{
82
    int i;
87
    int i;
83
   
88
   
84
    for (i = 0; str[i]; i++)
89
    for (i = 0; str[i]; i++)
85
        ;
90
        ;
86
   
91
   
87
    return i;
92
    return i;
88
}
93
}
89
 
94
 
90
/** Compare two NULL terminated strings
95
/** Compare two NULL terminated strings
91
 *
96
 *
92
 * Do a char-by-char comparison of two NULL terminated strings.
97
 * Do a char-by-char comparison of two NULL terminated strings.
93
 * The strings are considered equal iff they consist of the same
98
 * The strings are considered equal iff they consist of the same
94
 * characters on the minimum of their lengths and specified maximal
99
 * characters on the minimum of their lengths and specified maximal
95
 * length.
100
 * length.
96
 *
101
 *
97
 * @param src First string to compare.
102
 * @param src First string to compare.
98
 * @param dst Second string to compare.
103
 * @param dst Second string to compare.
99
 * @param len Maximal length for comparison.
104
 * @param len Maximal length for comparison.
100
 *
105
 *
101
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
106
 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
102
 *
107
 *
103
 */
108
 */
104
int strncmp(const char *src, const char *dst, size_t len)
109
int strncmp(const char *src, const char *dst, size_t len)
105
{
110
{
106
    int i;
111
    int i;
107
   
112
   
108
    i = 0;
113
    i = 0;
109
    for (;*src && *dst && i < len;src++,dst++,i++) {
114
    for (;*src && *dst && i < len;src++,dst++,i++) {
110
        if (*src < *dst)
115
        if (*src < *dst)
111
            return -1;
116
            return -1;
112
        if (*src > *dst)
117
        if (*src > *dst)
113
            return 1;
118
            return 1;
114
    }
119
    }
115
    if (i == len || *src == *dst)
120
    if (i == len || *src == *dst)
116
        return 0;
121
        return 0;
117
    if (!*src)
122
    if (!*src)
118
        return -1;
123
        return -1;
119
    return 1;
124
    return 1;
120
}
125
}
121
 
126
 
122
/** Copy NULL terminated string.
127
/** Copy NULL terminated string.
123
 *
128
 *
124
 * Copy at most 'len' characters from string 'src' to 'dest'.
129
 * Copy at most 'len' characters from string 'src' to 'dest'.
125
 * If 'src' is shorter than 'len', '\0' is inserted behind the
130
 * If 'src' is shorter than 'len', '\0' is inserted behind the
126
 * last copied character.
131
 * last copied character.
127
 *
132
 *
128
 * @param src Source string.
133
 * @param src Source string.
129
 * @param dst Destination buffer.
134
 * @param dst Destination buffer.
130
 * @param len Size of destination buffer.
135
 * @param len Size of destination buffer.
131
 */
136
 */
132
void strncpy(char *dest, const char *src, size_t len)
137
void strncpy(char *dest, const char *src, size_t len)
133
{
138
{
134
    int i;
139
    int i;
135
    for (i = 0; i < len; i++) {
140
    for (i = 0; i < len; i++) {
136
        if (!(dest[i] = src[i]))
141
        if (!(dest[i] = src[i]))
137
            return;
142
            return;
138
    }
143
    }
139
    dest[i-1] = '\0';
144
    dest[i-1] = '\0';
140
}
145
}
141
 
146
 
142
/** Convert ascii representation to __native
147
/** Convert ascii representation to __native
143
 *
148
 *
144
 * Supports 0x for hexa & 0 for octal notation.
149
 * Supports 0x for hexa & 0 for octal notation.
145
 * Does not check for overflows, does not support negative numbers
150
 * Does not check for overflows, does not support negative numbers
146
 *
151
 *
147
 * @param text Textual representation of number
152
 * @param text Textual representation of number
148
 * @return Converted number or 0 if no valid number ofund
153
 * @return Converted number or 0 if no valid number ofund
149
 */
154
 */
150
__native atoi(const char *text)
155
__native atoi(const char *text)
151
{
156
{
152
    int base = 10;
157
    int base = 10;
153
    __native result = 0;
158
    __native result = 0;
154
 
159
 
155
    if (text[0] == '0' && text[1] == 'x') {
160
    if (text[0] == '0' && text[1] == 'x') {
156
        base = 16;
161
        base = 16;
157
        text += 2;
162
        text += 2;
158
    } else if (text[0] == '0')
163
    } else if (text[0] == '0')
159
        base = 8;
164
        base = 8;
160
 
165
 
161
    while (*text) {
166
    while (*text) {
162
        if (base != 16 && \
167
        if (base != 16 && \
163
            ((*text >= 'A' && *text <= 'F' )
168
            ((*text >= 'A' && *text <= 'F' )
164
             || (*text >='a' && *text <='f')))
169
             || (*text >='a' && *text <='f')))
165
            break;
170
            break;
166
        if (base == 8 && *text >='8')
171
        if (base == 8 && *text >='8')
167
            break;
172
            break;
168
 
173
 
169
        if (*text >= '0' && *text <= '9') {
174
        if (*text >= '0' && *text <= '9') {
170
            result *= base;
175
            result *= base;
171
            result += *text - '0';
176
            result += *text - '0';
172
        } else if (*text >= 'A' && *text <= 'F') {
177
        } else if (*text >= 'A' && *text <= 'F') {
173
            result *= base;
178
            result *= base;
174
            result += *text - 'A' + 10;
179
            result += *text - 'A' + 10;
175
        } else if (*text >= 'a' && *text <= 'f') {
180
        } else if (*text >= 'a' && *text <= 'f') {
176
            result *= base;
181
            result *= base;
177
            result += *text - 'a' + 10;
182
            result += *text - 'a' + 10;
178
        } else
183
        } else
179
            break;
184
            break;
180
        text++;
185
        text++;
181
    }
186
    }
182
 
187
 
183
    return result;
188
    return result;
184
}
189
}
185
 
190