Subversion Repositories HelenOS

Rev

Rev 3100 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3100 Rev 4692
1
/*
1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
2
 * Copyright (c) 2008 Jiri Svoboda
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
/** @addtogroup debug
29
/** @addtogroup debug
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <stdlib.h>
35
#include <stdlib.h>
36
#include <libadt/list.h>
36
#include <adt/list.h>
37
#include <assert.h>
37
#include <assert.h>
38
#include <futex.h>
38
#include <futex.h>
39
#include <async.h>
39
#include <async.h>
40
 
40
 
41
#include "include/arch.h"
41
#include "include/arch.h"
42
#include "main.h"
42
#include "main.h"
43
#include "dthread.h"
43
#include "dthread.h"
44
 
44
 
45
static int last_thread_id = 0;
45
static int last_thread_id = 0;
46
 
46
 
47
link_t dthreads;        /* List of application's threads */
47
link_t dthreads;        /* List of application's threads */
48
dthread_t *cwt;         /* Current working thread */
48
dthread_t *cwt;         /* Current working thread */
49
 
49
 
50
dthread_t *dthread_new(thash_t hash)
50
dthread_t *dthread_new(thash_t hash)
51
{
51
{
52
    dthread_t *dt;
52
    dthread_t *dt;
53
 
53
 
54
    dt = malloc(sizeof(dthread_t));
54
    dt = malloc(sizeof(dthread_t));
55
    if (!dt) {
55
    if (!dt) {
56
        exit(1);
56
        exit(1);
57
    }
57
    }
58
 
58
 
59
    dt->id = ++last_thread_id;
59
    dt->id = ++last_thread_id;
60
    dt->hash = hash;
60
    dt->hash = hash;
61
    dt->fid = 0;
61
    dt->fid = 0;
62
    dt->stopped = 0;
62
    dt->stopped = 0;
63
 
63
 
64
    arch_dthread_initialize(dt);
64
    arch_dthread_initialize(dt);
65
 
65
 
66
    if (!cwt) cwt = dt;
66
    if (!cwt) cwt = dt;
67
 
67
 
68
    list_append(&dt->link, &dthreads);
68
    list_append(&dt->link, &dthreads);
69
 
69
 
70
    return dt;
70
    return dt;
71
}
71
}
72
 
72
 
73
void dthread_delete(dthread_t *dt)
73
void dthread_delete(dthread_t *dt)
74
{
74
{
75
    list_remove(&dt->link);
75
    list_remove(&dt->link);
76
    free(dt);
76
    free(dt);
77
}
77
}
78
 
78
 
79
/*
79
/*
80
 * Get dthread struct serviced by fibril fid.
80
 * Get dthread struct serviced by fibril fid.
81
 */
81
 */
82
dthread_t *dthread_get_by_fid(fid_t fid)
82
dthread_t *dthread_get_by_fid(fid_t fid)
83
{
83
{
84
    link_t *cur;
84
    link_t *cur;
85
    dthread_t *dt;
85
    dthread_t *dt;
86
 
86
 
87
    dt = NULL;
87
    dt = NULL;
88
    for (cur = dthreads.next; cur != &dthreads; cur = cur->next) {
88
    for (cur = dthreads.next; cur != &dthreads; cur = cur->next) {
89
        dt = list_get_instance(cur, dthread_t, link);
89
        dt = list_get_instance(cur, dthread_t, link);
90
        if (dt->fid == fid) return dt;
90
        if (dt->fid == fid) return dt;
91
    }  
91
    }  
92
 
92
 
93
    return NULL;
93
    return NULL;
94
}
94
}
95
 
95
 
96
/*
96
/*
97
 * Get dthread struct for dthread serviced by the current fibril.
97
 * Get dthread struct for dthread serviced by the current fibril.
98
 */
98
 */
99
dthread_t *dthread_get(void)
99
dthread_t *dthread_get(void)
100
{
100
{
101
    return dthread_get_by_fid(fibril_get_id());
101
    return dthread_get_by_fid(fibril_get_id());
102
}
102
}
103
 
103
 
104
void dthread_stop_me(void)
104
void dthread_stop_me(void)
105
{
105
{
106
    dthread_t *dt;
106
    dthread_t *dt;
107
 
107
 
108
    dt = dthread_get();
108
    dt = dthread_get();
109
    assert(dt);
109
    assert(dt);
110
 
110
 
111
    futex_down(&async_futex);
111
    futex_down(&async_futex);
112
 
112
 
113
    dt->stopped = 1;
113
    dt->stopped = 1;
114
    fibril_switch(FIBRIL_TO_MANAGER);
114
    fibril_switch(FIBRIL_TO_MANAGER);
115
 
115
 
116
    futex_up(&async_futex);
116
    futex_up(&async_futex);
117
}
117
}
118
 
118
 
119
void dthread_resume(dthread_t *dt)
119
void dthread_resume(dthread_t *dt)
120
{
120
{
121
    if (!dt->stopped) return;
121
    if (!dt->stopped) return;
122
 
122
 
123
    futex_down(&async_futex);
123
    futex_down(&async_futex);
124
 
124
 
125
    fibril_add_ready(dt->fid);
125
    fibril_add_ready(dt->fid);
126
    dt->stopped = 0;
126
    dt->stopped = 0;
127
 
127
 
128
    futex_up(&async_futex);
128
    futex_up(&async_futex);
129
}
129
}
130
 
130
 
131
unsigned dthread_get_pc(dthread_t *dt)
131
unsigned dthread_get_pc(dthread_t *dt)
132
{
132
{
133
    static istate_t istate;
133
    static istate_t istate;
134
    int rc;
134
    int rc;
135
 
135
 
136
    rc = udebug_regs_read(app_phone, dt->hash, &istate);
136
    rc = udebug_regs_read(app_phone, dt->hash, &istate);
137
    if (rc < 0) {
137
    if (rc < 0) {
138
        printf("failed reading registers (%d)\n", rc);
138
        printf("failed reading registers (%d)\n", rc);
139
        return 0;
139
        return 0;
140
    }
140
    }
141
 
141
 
142
    return istate_get_pc(&istate);
142
    return istate_get_pc(&istate);
143
}
143
}
144
 
144
 
145
/** @}
145
/** @}
146
 */
146
 */
147
 
147