This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmsgchan.c
224 lines (188 loc) · 3.59 KB
/
msgchan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "all.h"
#include <fcall.h>
#include <thread.h>
#include <9p.h> /* for emalloc9p */
enum
{
/* high bits of u.cx */
CmdOpen = 0x00,
CmdSendsize = 0x01,
CmdSenddata = 0x02,
CmdRecvsize = 0x03,
CmdRecvdata = 0x04,
CmdRecvstatus = 0x05,
CmdClose = 0x06,
StatSuccess = 0x0001, /* request succeeded */
StatHavedata = 0x0002, /* vmware has message available */
StatClosed = 0x0004, /* channel got closed */
StatUnsent = 0x0008, /* vmware removed message before it got delivered */
StatChkpt = 0x0010, /* checkpoint occurred */
StatPoweroff = 0x0020, /* underlying device is powering off */
};
Msgchan*
openmsg(ulong proto)
{
Msgchan *c;
Ureg u;
memset(&u, 0, sizeof u);
u.cx = (CmdOpen<<16) | BackMessage;
u.bx = proto;
backdoor(&u, 0);
fprint(2, "msgopen %.8lux\n", u.cx);
if(!(u.cx & (StatSuccess<<16))){
fprint(2, "message %.8lux\n", u.cx);
werrstr("unable to open message channel (%.8lux)", u.cx);
return nil;
}
c = emalloc9p(sizeof(*c));
c->id = u.dx>>16;
return c;
}
enum
{
Ok = 0,
ErrBad = -1,
ErrChkpt = -2,
};
static int
sendpiece(Msgchan *c, int ty, ulong a)
{
Ureg u;
memset(&u, 0, sizeof u);
u.cx = (ty<<16) | BackMessage;
u.dx = c->id<<16;
u.bx = a;
backdoor(&u, 0);
if(!(u.cx & (StatSuccess<<16))){
fprint(2, "cmd %x failed\n", ty);
if(u.cx & (StatChkpt<<16))
return ErrChkpt;
return ErrBad;
}
return 0;
}
int
sendmsg(Msgchan *c, void *a, int n)
{
int i, m, e, left;
uchar *p;
ulong v;
Again:
v = n;
if(sendpiece(c, CmdSendsize, v) < 0)
return -1;
p = a;
left = n;
while(left > 0){
m = left;
if(m > 4)
m = 4;
v = 0;
for(i=0; i<m; i++)
v |= *p++ << (8*i);
if((e=sendpiece(c, CmdSenddata, v)) < 0){
if(e == -2)
goto Again;
return -1;
}
left -= m;
}
return 0;
}
static int
recvpiece(Msgchan *c, int ty, ulong *stat, ulong *a)
{
Ureg u;
fprint(2, "recvpiece %d %d\n", c->id, ty);
memset(&u, 0, sizeof u);
u.cx = (ty<<16) | BackMessage;
u.bx = StatSuccess;
u.dx = c->id<<16;
backdoor(&u, 0);
if(!(u.cx & (StatSuccess<<16))){
fprint(2, "no success %lux\n", u.cx);
if(u.cx & (StatChkpt<<16))
return ErrChkpt;
return ErrBad;
}
*stat = u.cx;
if(ty == CmdRecvsize && !(u.cx&(StatHavedata<<16))){
fprint(2, "poll got no data\n");
return 0;
}
if(ty != CmdRecvstatus && (u.dx>>16) != ty-2){
fprint(2, "got wrong answer! %lux\n", u.dx);
werrstr("protocol error");
return ErrBad;
}
*a = u.bx;
fprint(2, "got %lux\n", *a);
return 0;
}
static void
signalerror(Msgchan *c, int ty)
{
Ureg u;
memset(&u, 0, sizeof u);
u.cx = (ty<<16) | BackMessage;
u.dx = c->id<<16;
u.bx = 0;
backdoor(&u, 0);
}
int
recvmsg(Msgchan *c, void **pp)
{
int left;
ulong v, stat, tot;
uchar *p;
Top:
if(recvpiece(c, CmdRecvsize, &stat, &tot) < 0)
return -1;
if(!(stat & (StatHavedata<<16)))
return 0;
free(c->a);
c->a = emalloc9p(tot+5);
c->na = tot;
p = c->a; /* NUL terminate for callers */
left = tot;
while(left > 0){
switch(recvpiece(c, CmdRecvdata, &stat, &v)){
case ErrChkpt:
goto Top;
case ErrBad:
signalerror(c, CmdRecvdata);
return -1;
}
*(uint*)p = v;
p += 4;
left -= 4;
}
((char*)c->a)[tot] = '\0';
switch(recvpiece(c, CmdRecvstatus, &stat, &v)){
case ErrChkpt:
goto Top;
case ErrBad:
/* BUG: signal receipt of error */
signalerror(c, CmdRecvstatus);
return -1;
}
*pp = c->a;
return tot;
}
int
closemsg(Msgchan *c)
{
Ureg u;
memset(&u, 0, sizeof u);
u.cx = (CmdClose<<16) | BackMessage;
u.dx = c->id;
backdoor(&u, 0);
if(!(u.cx & (StatSuccess<<16))){
werrstr("unable to close message channel");
return -1;
}
free(c->a);
c->a = nil;
free(c);
return 0;
}