-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.c
427 lines (365 loc) · 13.3 KB
/
context.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include "doomdev2.h"
#include "hd2.h"
#include "hd2_buffer.h"
#include "common.h"
#include "context.h"
/* Maximum number of commands sent to the device in a batch. */
#define MAX_BATCH_CMDS 1024
#define SSIZE_MAX LONG_MAX
_Static_assert(sizeof(ssize_t) == sizeof(long), "ssize_t");
/* Since we return the number of written bytes in context_write using a ssize_t,
we can only write as many bytes as ssize_t can hold. */
#define MAX_CMDS (SSIZE_MAX / sizeof(struct doomdev2_cmd))
struct context {
struct harddoom2* hd2;
struct hd2_buffer* curr_bufs[NUM_USER_BUFS];
struct doomdev2_cmd cmds[MAX_BATCH_CMDS];
struct mutex mut;
};
static int context_open(struct inode* inode, struct file* file) {
unsigned number = MINOR(inode->i_rdev);
if (number >= DEVICES_LIMIT) {
DEBUG("invalid minor");
return -EINVAL;
}
struct context* ctx = kzalloc(sizeof(struct context), GFP_KERNEL);
if (!ctx) {
DEBUG("ctx: kmalloc");
return -ENOMEM;
}
ctx->hd2 = get_hd2(number);
mutex_init(&ctx->mut);
file->private_data = ctx;
return 0;
}
static int context_release(struct inode* inode, struct file* file) {
struct context* ctx = (struct context*)file->private_data;
release_user_bufs(ctx->curr_bufs);
kfree(ctx);
return 0;
}
static int setup(struct context* ctx, struct doomdev2_ioctl_setup __user* _params) {
struct doomdev2_ioctl_setup params;
if (copy_from_user(¶ms, _params, sizeof(struct doomdev2_ioctl_setup))) {
DEBUG("setup copy_from_user fail");
return -EFAULT;
}
int32_t fds[NUM_USER_BUFS] = { params.surf_dst_fd, params.surf_src_fd, params.texture_fd,
params.flat_fd, params.translation_fd, params.colormap_fd, params.tranmap_fd };
struct hd2_buffer* bufs[NUM_USER_BUFS] = { NULL };
int err;
int i;
for (i = 0; i < NUM_USER_BUFS; ++i) {
if (fds[i] == -1) continue;
bufs[i] = hd2_buff_fd_get(fds[i]);
if (IS_ERR(bufs[i])) {
DEBUG("setup: wrong fd");
err = PTR_ERR(bufs[i]);
goto out_fds;
}
}
err = -EINVAL;
int j;
for (j = 0; j < 2; ++j) {
if (!bufs[j]) continue;
if (!is_surface(bufs[j])) {
DEBUG("setup: non-surface given as surface");
goto out_fds;
}
}
for (; j < NUM_USER_BUFS; ++j) {
if (!bufs[j]) continue;
BUG_ON(!get_buff_size(bufs[j]));
if (is_surface(bufs[j])) {
DEBUG("setup: surface given as non-surface");
goto out_fds;
}
}
for (j = 0; j < NUM_USER_BUFS; ++j) {
if (!bufs[j]) continue;
if (!assigned_to(bufs[j], ctx->hd2)) {
DEBUG("setup: wrong device");
goto out_fds;
}
}
if (bufs[DST_BUF_IDX] && bufs[SRC_BUF_IDX] &&
(get_buff_width(bufs[DST_BUF_IDX]) != get_buff_width(bufs[SRC_BUF_IDX]) ||
get_buff_height(bufs[DST_BUF_IDX]) != get_buff_height(bufs[SRC_BUF_IDX]))) {
/* The only command that uses the the source buffer, COPY_RECT, assumes they have the same dimensions. */
DEBUG("setup: different dst and src buf dims");
goto out_fds;
}
if (bufs[FLAT_BUF_IDX] && get_buff_size(bufs[FLAT_BUF_IDX]) % (1 << 12)) {
DEBUG("setup: flat buf wrong size");
goto out_fds;
}
if (bufs[TRANSLATE_BUF_IDX] && get_buff_size(bufs[TRANSLATE_BUF_IDX]) % (1 << 8)) {
DEBUG("setup: translate buf wrong size");
goto out_fds;
}
if (bufs[COLORMAP_BUF_IDX] && get_buff_size(bufs[COLORMAP_BUF_IDX]) % (1 << 8)) {
DEBUG("setup: colormap buf wrong size");
goto out_fds;
}
if (bufs[TRANMAP_BUF_IDX] && get_buff_size(bufs[TRANMAP_BUF_IDX]) % (1 << 16)) {
DEBUG("setup: tranmap buf wrong size");
goto out_fds;
}
mutex_lock(&ctx->mut);
for (j = 0; j < NUM_USER_BUFS; ++j) {
hd2_buff_put(ctx->curr_bufs[j]);
ctx->curr_bufs[j] = bufs[j];
}
mutex_unlock(&ctx->mut);
return 0;
out_fds:
for (--i; i >= 0; --i) {
hd2_buff_put(bufs[i]);
}
return err;
}
static long context_ioctl(struct file* file, unsigned cmd, unsigned long arg) {
struct context* ctx = (struct context*)file->private_data;
switch (cmd) {
case DOOMDEV2_IOCTL_CREATE_SURFACE:
return harddoom2_create_surface(ctx->hd2, (struct doomdev2_ioctl_create_surface __user*)arg);
case DOOMDEV2_IOCTL_CREATE_BUFFER:
return harddoom2_create_buffer(ctx->hd2, (struct doomdev2_ioctl_create_buffer __user*)arg);
case DOOMDEV2_IOCTL_SETUP:
return setup(ctx, (struct doomdev2_ioctl_setup __user*)arg);
}
return -ENOTTY;
}
static bool validate_maps(struct context* ctx, uint8_t flags, uint16_t colormap_idx, uint16_t translation_idx) {
if (flags & DOOMDEV2_CMD_FLAGS_TRANSLATE && !ctx->curr_bufs[TRANSLATE_BUF_IDX]) {
DEBUG("draw_column: translate flag set but no buf");
return false;
}
if (flags & DOOMDEV2_CMD_FLAGS_COLORMAP && !ctx->curr_bufs[COLORMAP_BUF_IDX]) {
DEBUG("draw_column: colormap flag set but no buf");
return false;
}
if (flags & DOOMDEV2_CMD_FLAGS_TRANMAP && !ctx->curr_bufs[TRANMAP_BUF_IDX]) {
DEBUG("draw_column: tranmap flag set but no buf");
return false;
}
if (flags & DOOMDEV2_CMD_FLAGS_TRANSLATE
&& translation_idx >= (get_buff_size(ctx->curr_bufs[TRANSLATE_BUF_IDX]) >> 8)) {
DEBUG("draw_column: translation idx out of bounds");
return false;
}
if (flags & DOOMDEV2_CMD_FLAGS_COLORMAP
&& colormap_idx >= (get_buff_size(ctx->curr_bufs[COLORMAP_BUF_IDX]) >> 8)) {
DEBUG("draw_column: colormap idx out of bounds");
return false;
}
return true;
}
static bool validate_cmd(struct context* ctx, const struct doomdev2_cmd* user_cmd) {
BUG_ON(!ctx->curr_bufs[DST_BUF_IDX]);
struct hd2_buffer* dst_buff = ctx->curr_bufs[DST_BUF_IDX];
uint16_t surf_width = get_buff_width(dst_buff), surf_height = get_buff_height(dst_buff);
{
struct hd2_buffer* src_buff = ctx->curr_bufs[SRC_BUF_IDX];
BUG_ON(src_buff && (get_buff_width(src_buff) != surf_width || get_buff_height(src_buff) != surf_height));
}
switch (user_cmd->type) {
case DOOMDEV2_CMD_TYPE_COPY_RECT: {
if (!ctx->curr_bufs[SRC_BUF_IDX]) {
DEBUG("copy rect: no src buf");
return false;
}
const struct doomdev2_cmd_copy_rect* cmd = &user_cmd->copy_rect;
if ((uint32_t)cmd->pos_dst_x + cmd->width > surf_width ||
(uint32_t)cmd->pos_dst_y + cmd->height > surf_height ||
(uint32_t)cmd->pos_src_x + cmd->width > surf_width ||
(uint32_t)cmd->pos_src_y + cmd->height > surf_height) {
DEBUG("copy_rect: out of bounds");
return false;
}
if (ctx->curr_bufs[DST_BUF_IDX] == ctx->curr_bufs[SRC_BUF_IDX] &&
cmd->pos_dst_x < cmd->pos_src_x + cmd->width && cmd->pos_dst_x + cmd->width > cmd->pos_src_x &&
cmd->pos_dst_y < cmd->pos_src_y + cmd->height && cmd->pos_dst_y + cmd->height > cmd->pos_src_y) {
DEBUG("copy_rect: intersection");
return false;
}
return true;
}
case DOOMDEV2_CMD_TYPE_FILL_RECT: {
const struct doomdev2_cmd_fill_rect* cmd = &user_cmd->fill_rect;
if ((uint32_t)cmd->pos_x + cmd->width > surf_width || (uint32_t)cmd->pos_y + cmd->height > surf_height) {
DEBUG("fill_rect: out of bounds");
return false;
}
return true;
}
case DOOMDEV2_CMD_TYPE_DRAW_LINE: {
const struct doomdev2_cmd_draw_line* cmd = &user_cmd->draw_line;
if (cmd->pos_a_x >= surf_width || cmd->pos_a_y >= surf_height
|| cmd->pos_b_x >= surf_width || cmd->pos_b_y >= surf_height) {
DEBUG("draw_line: out of bounds");
return false;
}
return true;
}
case DOOMDEV2_CMD_TYPE_DRAW_BACKGROUND: {
if (!ctx->curr_bufs[FLAT_BUF_IDX]) {
DEBUG("draw background: no flat buf");
return false;
}
const struct doomdev2_cmd_draw_background* cmd = &user_cmd->draw_background;
if ((uint32_t)cmd->pos_x + cmd->width > surf_width || (uint32_t)cmd->pos_y + cmd->height > surf_height) {
DEBUG("draw background: out of bounds");
return false;
}
if (cmd->flat_idx >= (get_buff_size(ctx->curr_bufs[FLAT_BUF_IDX]) >> 12)) {
DEBUG("draw background: flat idx out of bounds");
return false;
}
return true;
}
case DOOMDEV2_CMD_TYPE_DRAW_COLUMN: {
if (!ctx->curr_bufs[TEXTURE_BUF_IDX]) {
DEBUG("draw_column: no texture buffer");
return false;
}
const struct doomdev2_cmd_draw_column* cmd = &user_cmd->draw_column;
if (cmd->pos_x >= surf_width || cmd->pos_b_y >= surf_height) {
DEBUG("draw_column: out of bounds");
return false;
}
if (cmd->pos_b_y < cmd->pos_a_y) {
DEBUG("draw_column: b_y < a_y");
return false;
}
if (!validate_maps(ctx, cmd->flags, cmd->colormap_idx, cmd->translation_idx)) {
return false;
}
return true;
}
case DOOMDEV2_CMD_TYPE_DRAW_SPAN: {
if (!ctx->curr_bufs[FLAT_BUF_IDX]) {
DEBUG("draw span: no flat buffer");
return false;
}
const struct doomdev2_cmd_draw_span* cmd = &user_cmd->draw_span;
if (cmd->pos_y >= surf_height || cmd->pos_b_x >= surf_width) {
DEBUG("draw span: out of bounds");
return false;
}
if (cmd->pos_b_x < cmd->pos_a_x) {
DEBUG("draw span: b_x < a_x");
return false;
}
if (cmd->flat_idx >= (get_buff_size(ctx->curr_bufs[FLAT_BUF_IDX]) >> 12)) {
DEBUG("draw span: flat idx out of bounds");
return false;
}
if (!validate_maps(ctx, cmd->flags, cmd->colormap_idx, cmd->translation_idx)) {
return false;
}
return true;
}
case DOOMDEV2_CMD_TYPE_DRAW_FUZZ: {
if (!ctx->curr_bufs[COLORMAP_BUF_IDX]) {
DEBUG("draw fuzz: no colormap");
return false;
}
const struct doomdev2_cmd_draw_fuzz* cmd = &user_cmd->draw_fuzz;
if (cmd->pos_x >= surf_width || cmd->fuzz_end >= surf_height) {
DEBUG("draw fuzz: out of bounds");
return false;
}
if (cmd->fuzz_start > cmd->pos_a_y || cmd->pos_a_y > cmd->pos_b_y || cmd->pos_b_y > cmd->fuzz_end) {
DEBUG("draw fuzz: inequalities");
return false;
}
if (cmd->colormap_idx >= (get_buff_size(ctx->curr_bufs[COLORMAP_BUF_IDX]) >> 8)) {
DEBUG("draw fuzz: colormap idx out of bounds");
return false;
}
if (cmd->fuzz_pos > 55) {
DEBUG("draw fuzz: fuzz_pos > 55");
return false;
}
return true;
}
}
DEBUG("unknown cmd type: %u", user_cmd->type);
return false;
}
static ssize_t context_write(struct file* file, const char __user* _buf, size_t count, loff_t* off) {
if (!count || count % sizeof(struct doomdev2_cmd) != 0) {
DEBUG("context_write: wrong count %lu", count);
return -EINVAL;
}
size_t num_cmds = count / sizeof(struct doomdev2_cmd);
if (num_cmds > MAX_CMDS) {
num_cmds = MAX_CMDS;
}
struct context* ctx = (struct context*)file->private_data;
int err = 0;
size_t cmds_written = 0;
mutex_lock(&ctx->mut);
if (!ctx->curr_bufs[DST_BUF_IDX]) {
DEBUG("write: no dst surface set");
err = -EINVAL;
goto out_surf;
}
size_t num_batch = MAX_BATCH_CMDS;
while (num_cmds) {
if (num_batch > num_cmds) {
/* Last batch. */
num_batch = num_cmds;
}
if (copy_from_user(ctx->cmds, _buf, num_batch * sizeof(struct doomdev2_cmd))) {
DEBUG("context_write: copy from user fail");
err = -EFAULT;
break;
}
size_t it;
for (it = 0; it < num_batch; ++it) {
if (!validate_cmd(ctx, &ctx->cmds[it])) {
break;
}
}
if (!it) {
/* The first command in this batch was invalid. */
err = -EINVAL;
break;
}
err = harddoom2_write(ctx->hd2, ctx->curr_bufs, ctx->cmds, it);
if (err < 0) {
break;
}
BUG_ON(!err || err > it);
cmds_written += err;
if (it < num_batch) {
/* One of the middle commands was invalid. */
break;
}
/* The whole batch was successfuly written. */
num_cmds -= num_batch;
_buf += num_batch * sizeof(struct doomdev2_cmd);
}
out_surf:
mutex_unlock(&ctx->mut);
if (cmds_written) {
return cmds_written * sizeof(struct doomdev2_cmd);
} else {
BUG_ON(!err);
return err;
}
}
const struct file_operations _context_ops = {
.owner = THIS_MODULE,
.open = context_open,
.release = context_release,
.unlocked_ioctl = context_ioctl,
.compat_ioctl = context_ioctl,
.write = context_write
};
const struct file_operations* const context_ops = &_context_ops;