summaryrefslogtreecommitdiffstats
path: root/message.asm
blob: 3d2170c4aa8800d99ab29b7cbfa3b4097005b2ba (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* -*- asm -*- ----------------------------------------------------------- *
 *   
 *   Copyright 2013 H. Peter Anvin - All Rights Reserved
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *   
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *   
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------- */

#include "v9fs.h"
	.code16
	
/* ------------------------------------------------------------------------- *
 *  Transmit and receive a virtio message (in msgbuf), including some
 *  handling of common operations.
 *
 *  On entry:
 *  message (minus length, opcode, tag) starting at msgbuf+7
 *  %di -> first unused byte in msgbuf (used to fill in the size field)
 *  %al -> message opcode
 *  %ah -> 0 = no data, 1 = write data, 2 = read data
 *  %dx -> receive message header length for read data
 *  %es:%bx -> data buffer if read or write
 *  %cx -> data length if read or write
 *
 *  On return:
 *  reply in msgbuf, %di -> msgbuf+7 (= payload)
 *  carry set if error reply or protocol error, error code in %ax
 * ------------------------------------------------------------------------- */

	/* We may need up to 17 pages for a 64K data transfer */
	lrbuf data_edds, (0x10 + 8*17), 8
de_size		= data_edds
de_offset	= data_edds + 0x4
de_seg		= data_edds + 0x8
de_avail	= data_edds + 0xc
de_used		= data_edds + 0xe
de_ranges	= data_edds + 0x10

	.section ".rtext","ax"

	/* The VERSION message must have tag 0xffff by protocol */
	grdword	last_tag 0xfffe

GPROC(simple_message)
	xorb %ah,%ah		/* No data in or out */
	/* fall through */
END(simple_message)

GPROC(do_message)
	/* VDS input entries */
	
	movw %bx,de_offset
	movw %es,de_seg
	movw %cx,de_size
	movb $17,de_avail

	/* This code installs the default physical == linear EDDS */

	movb $1,de_used
	movw %cx,de_ranges+4	/* Size of region */
	movzwl de_seg,%ecx
	shll $4,%ecx
	movzwl %bx,%ebx
	addl %ebx,%ecx
	movl %ecx,de_ranges	/* Linear/physical address */

	testb have_vds,%ah
	jz 1f			/* No VDS or no data */

	pushal
	movw $0x8105,%ax	/* Scatter/gather lock for DMA */
	xorw %dx,%dx
	movw $data_edds,%di
	int $0x4b
	popal
	
1:
	/* Tweak message header and compute length */
	movzwl %di,%edi
	movw msgbuf,%si
	subw %si,%di
	movl %edi,(%si)		/* Message length */
	movw last_tag,%cx
	incw %cx
	movw %cx,last_tag
	movb %al,4(%si)		/* Message opcode */
	movw %cx,5(%si)		/* Message tag */

#ifdef DEBUG
	/* Dump message */
	pushaw
	debugmsg "\r\n> "
	movw %di,%cx
411:
	lodsb
	pushw %ax
	call debug_printhex2
	loopw 411b
	call debug_crlf
	popaw
#endif
	
	/* Now build the descriptors */
	
	movw vring_desc,%si
	
	movl msgbuf_paddr,%ebx
	movl %ebx,(%si)		/* Physical address of msgbuf */
	movw %di,8(%si)		/* Length of message */
	movb $0,12(%si)		/* Flags: read, not (yet) chained */
	addw $16,%si

	testb $1,%ah
	jz 1f
	call edds_to_virtio
1:

	orb $1,-4(%si)		/* Previous descriptor now chained */
	movl %ebx,(%si)		/* Physical address of msgbuf */
	testb $2,%ah
	jnz 2f
	movw msgbuf_size,%dx
2:
	movw %dx,8(%si)		/* Buffer space */
	movb $2,12(%si)		/* Flags: write, not (yet) chained */

	testb $2,%ah
	jz 3f
	call edds_to_virtio
3:

	pushw %ax		/* Save data/opcode */
	
	/* Now all the descriptors are set up and the data is mapped */
	movw vring_avail,%si
	movw 2(%si),%cx		/* Get the current avail index */
	incw %cx		/* Bump it by one */
	andw vring_mask,%cx	/* ... wrap around */

	/* Do it! */
	movw vring_kicker,%dx
	xorw %ax,%ax
	movw %cx,2(%si)		/* Bump the avail ring pointer */
	outw %ax,(%dx)		/* Kick the ring */

	/* Wait for the other side */
	movw vring_used,%si
4:
	cmpw 2(%si),%cx		/* Wait for the used index to match */
	je 5f
	rep;nop			/* Can we do better here?  HLT? */
	jmp 4b
5:
	/* Now we have a response */
	popw %dx		/* Restore data/opcode */

#ifdef DEBUG
	/* Dump the response */
	pushaw
	debugmsg "< "
	movw msgbuf,%si
	movw (%si),%cx
412:
	lodsb
	pushw %ax
	call debug_printhex2
	loopw 412b
	call debug_crlf
	popaw
#endif
	
	testb have_vds,%dh
	jz 6f			/* No VDS or no data */

	pushal
	movw $0x8106,%ax	/* Scatter/gather unlock for DMA */
	xorw %dx,%dx
	movw $data_edds,%di
	int $0x4b
	popal
6:

	movw $L_EIO,%ax		/* Error code = EIO */
	
	movw msgbuf,%di
	cmpl $7,(%di)		/* Length + opcode + tag = 7 bytes */
	jb error		/* Reply impossibly small */

	movw last_tag,%cx	/* Tag must match! */
	cmpw 5(%di),%cx
	jne error

	incw %dx
	cmpb 4(%di),%dl		/* Reply opcode = request opcode + 1 */
	je ok			/* CF = 0 if branch taken */

msg_error:
	cmpb $P9_RLERROR,4(%di)
	jne error
	movw 7(%di),%ax		/* Linux errno */
error:
	stc
ok:
	addw $7,%di		/* CF <- 0, %di <- payload */
	retw
END(do_message)
	
/* ------------------------------------------------------------------------- *
 *  Convert the EDDS to virtio descriptor format
 *
 *  %si points to the first unused virtio descriptor both before & after
 *  %ah = 1 for output, 2 for input
 * ------------------------------------------------------------------------- */
LPROC(edds_to_virtio)
	pushl %edx
	pushw %ax
	pushw %cx
	pushw %di
	movw de_used,%cx
	movw $de_ranges,%di
	andb $2,%ah		/* 0 for output, 2 for input */
	
1:
	movl (%di),%edx		/* Physical address */
	movl %edx,(%si)
	orb $1,-4(%si)		/* Set link bit of previous descriptor */
	movw 4(%di),%dx		/* Length */
	movw %dx,8(%si)
	movb %ah,12(%si)	/* Flags */

	addw $8,%di
	addw $16,%si
	
	loopw 1b

	popw %di
	popw %cx
	popw %ax
	popl %edx
	retw
END(edds_to_virtio)