5113f6f70
김현기
kernel add
|
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
/*
* Standard user space access functions based on mvcp/mvcs and doing
* interesting things in the secondary space mode.
*
* Copyright IBM Corp. 2006,2014
* Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
* Gerald Schaefer (gerald.schaefer@de.ibm.com)
*/
#include <linux/jump_label.h>
#include <linux/uaccess.h>
#include <linux/export.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <asm/mmu_context.h>
#include <asm/facility.h>
static DEFINE_STATIC_KEY_FALSE(have_mvcos);
static inline unsigned long copy_from_user_mvcos(void *x, const void __user *ptr,
unsigned long size)
{
register unsigned long reg0 asm("0") = 0x81UL;
unsigned long tmp1, tmp2;
tmp1 = -4096UL;
asm volatile(
"0: .insn ss,0xc80000000000,0(%0,%2),0(%1),0
"
"9: jz 7f
"
"1: algr %0,%3
"
" slgr %1,%3
"
" slgr %2,%3
"
" j 0b
"
"2: la %4,4095(%1)
"/* %4 = ptr + 4095 */
" nr %4,%3
" /* %4 = (ptr + 4095) & -4096 */
" slgr %4,%1
"
" clgr %0,%4
" /* copy crosses next page boundary? */
" jnh 4f
"
"3: .insn ss,0xc80000000000,0(%4,%2),0(%1),0
"
"10:slgr %0,%4
"
" algr %2,%4
"
"4: lghi %4,-1
"
" algr %4,%0
" /* copy remaining size, subtract 1 */
" bras %3,6f
" /* memset loop */
" xc 0(1,%2),0(%2)
"
"5: xc 0(256,%2),0(%2)
"
" la %2,256(%2)
"
"6: aghi %4,-256
"
" jnm 5b
"
" ex %4,0(%3)
"
" j 8f
"
"7:slgr %0,%0
"
"8:
"
EX_TABLE(0b,2b) EX_TABLE(3b,4b) EX_TABLE(9b,2b) EX_TABLE(10b,4b)
: "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
: "d" (reg0) : "cc", "memory");
return size;
}
static inline unsigned long copy_from_user_mvcp(void *x, const void __user *ptr,
unsigned long size)
{
unsigned long tmp1, tmp2;
load_kernel_asce();
tmp1 = -256UL;
asm volatile(
" sacf 0
"
"0: mvcp 0(%0,%2),0(%1),%3
"
"10:jz 8f
"
"1: algr %0,%3
"
" la %1,256(%1)
"
" la %2,256(%2)
"
"2: mvcp 0(%0,%2),0(%1),%3
"
"11:jnz 1b
"
" j 8f
"
"3: la %4,255(%1)
" /* %4 = ptr + 255 */
" lghi %3,-4096
"
" nr %4,%3
" /* %4 = (ptr + 255) & -4096 */
" slgr %4,%1
"
" clgr %0,%4
" /* copy crosses next page boundary? */
" jnh 5f
"
"4: mvcp 0(%4,%2),0(%1),%3
"
"12:slgr %0,%4
"
" algr %2,%4
"
"5: lghi %4,-1
"
" algr %4,%0
" /* copy remaining size, subtract 1 */
" bras %3,7f
" /* memset loop */
" xc 0(1,%2),0(%2)
"
"6: xc 0(256,%2),0(%2)
"
" la %2,256(%2)
"
"7: aghi %4,-256
"
" jnm 6b
"
" ex %4,0(%3)
"
" j 9f
"
"8:slgr %0,%0
"
"9: sacf 768
"
EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,5b)
EX_TABLE(10b,3b) EX_TABLE(11b,3b) EX_TABLE(12b,5b)
: "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
: : "cc", "memory");
return size;
}
unsigned long __copy_from_user(void *to, const void __user *from, unsigned long n)
{
if (static_branch_likely(&have_mvcos))
return copy_from_user_mvcos(to, from, n);
return copy_from_user_mvcp(to, from, n);
}
EXPORT_SYMBOL(__copy_from_user);
static inline unsigned long copy_to_user_mvcos(void __user *ptr, const void *x,
unsigned long size)
{
register unsigned long reg0 asm("0") = 0x810000UL;
unsigned long tmp1, tmp2;
tmp1 = -4096UL;
asm volatile(
"0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0
"
"6: jz 4f
"
"1: algr %0,%3
"
" slgr %1,%3
"
" slgr %2,%3
"
" j 0b
"
"2: la %4,4095(%1)
"/* %4 = ptr + 4095 */
" nr %4,%3
" /* %4 = (ptr + 4095) & -4096 */
" slgr %4,%1
"
" clgr %0,%4
" /* copy crosses next page boundary? */
" jnh 5f
"
"3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0
"
"7: slgr %0,%4
"
" j 5f
"
"4: slgr %0,%0
"
"5:
"
EX_TABLE(0b,2b) EX_TABLE(3b,5b) EX_TABLE(6b,2b) EX_TABLE(7b,5b)
: "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
: "d" (reg0) : "cc", "memory");
return size;
}
static inline unsigned long copy_to_user_mvcs(void __user *ptr, const void *x,
unsigned long size)
{
unsigned long tmp1, tmp2;
load_kernel_asce();
tmp1 = -256UL;
asm volatile(
" sacf 0
"
"0: mvcs 0(%0,%1),0(%2),%3
"
"7: jz 5f
"
"1: algr %0,%3
"
" la %1,256(%1)
"
" la %2,256(%2)
"
"2: mvcs 0(%0,%1),0(%2),%3
"
"8: jnz 1b
"
" j 5f
"
"3: la %4,255(%1)
" /* %4 = ptr + 255 */
" lghi %3,-4096
"
" nr %4,%3
" /* %4 = (ptr + 255) & -4096 */
" slgr %4,%1
"
" clgr %0,%4
" /* copy crosses next page boundary? */
" jnh 6f
"
"4: mvcs 0(%4,%1),0(%2),%3
"
"9: slgr %0,%4
"
" j 6f
"
"5: slgr %0,%0
"
"6: sacf 768
"
EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b)
EX_TABLE(7b,3b) EX_TABLE(8b,3b) EX_TABLE(9b,6b)
: "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
: : "cc", "memory");
return size;
}
unsigned long __copy_to_user(void __user *to, const void *from, unsigned long n)
{
if (static_branch_likely(&have_mvcos))
return copy_to_user_mvcos(to, from, n);
return copy_to_user_mvcs(to, from, n);
}
EXPORT_SYMBOL(__copy_to_user);
static inline unsigned long copy_in_user_mvcos(void __user *to, const void __user *from,
unsigned long size)
{
register unsigned long reg0 asm("0") = 0x810081UL;
unsigned long tmp1, tmp2;
tmp1 = -4096UL;
/* FIXME: copy with reduced length. */
asm volatile(
"0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0
"
" jz 2f
"
"1: algr %0,%3
"
" slgr %1,%3
"
" slgr %2,%3
"
" j 0b
"
"2:slgr %0,%0
"
"3:
"
EX_TABLE(0b,3b)
: "+a" (size), "+a" (to), "+a" (from), "+a" (tmp1), "=a" (tmp2)
: "d" (reg0) : "cc", "memory");
return size;
}
static inline unsigned long copy_in_user_mvc(void __user *to, const void __user *from,
unsigned long size)
{
unsigned long tmp1;
load_kernel_asce();
asm volatile(
" sacf 256
"
" aghi %0,-1
"
" jo 5f
"
" bras %3,3f
"
"0: aghi %0,257
"
"1: mvc 0(1,%1),0(%2)
"
" la %1,1(%1)
"
" la %2,1(%2)
"
" aghi %0,-1
"
" jnz 1b
"
" j 5f
"
"2: mvc 0(256,%1),0(%2)
"
" la %1,256(%1)
"
" la %2,256(%2)
"
"3: aghi %0,-256
"
" jnm 2b
"
"4: ex %0,1b-0b(%3)
"
"5: slgr %0,%0
"
"6: sacf 768
"
EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
: "+a" (size), "+a" (to), "+a" (from), "=a" (tmp1)
: : "cc", "memory");
return size;
}
unsigned long __copy_in_user(void __user *to, const void __user *from, unsigned long n)
{
if (static_branch_likely(&have_mvcos))
return copy_in_user_mvcos(to, from, n);
return copy_in_user_mvc(to, from, n);
}
EXPORT_SYMBOL(__copy_in_user);
static inline unsigned long clear_user_mvcos(void __user *to, unsigned long size)
{
register unsigned long reg0 asm("0") = 0x810000UL;
unsigned long tmp1, tmp2;
tmp1 = -4096UL;
asm volatile(
"0: .insn ss,0xc80000000000,0(%0,%1),0(%4),0
"
" jz 4f
"
"1: algr %0,%2
"
" slgr %1,%2
"
" j 0b
"
"2: la %3,4095(%1)
"/* %4 = to + 4095 */
" nr %3,%2
" /* %4 = (to + 4095) & -4096 */
" slgr %3,%1
"
" clgr %0,%3
" /* copy crosses next page boundary? */
" jnh 5f
"
"3: .insn ss,0xc80000000000,0(%3,%1),0(%4),0
"
" slgr %0,%3
"
" j 5f
"
"4:slgr %0,%0
"
"5:
"
EX_TABLE(0b,2b) EX_TABLE(3b,5b)
: "+a" (size), "+a" (to), "+a" (tmp1), "=a" (tmp2)
: "a" (empty_zero_page), "d" (reg0) : "cc", "memory");
return size;
}
static inline unsigned long clear_user_xc(void __user *to, unsigned long size)
{
unsigned long tmp1, tmp2;
load_kernel_asce();
asm volatile(
" sacf 256
"
" aghi %0,-1
"
" jo 5f
"
" bras %3,3f
"
" xc 0(1,%1),0(%1)
"
"0: aghi %0,257
"
" la %2,255(%1)
" /* %2 = ptr + 255 */
" srl %2,12
"
" sll %2,12
" /* %2 = (ptr + 255) & -4096 */
" slgr %2,%1
"
" clgr %0,%2
" /* clear crosses next page boundary? */
" jnh 5f
"
" aghi %2,-1
"
"1: ex %2,0(%3)
"
" aghi %2,1
"
" slgr %0,%2
"
" j 5f
"
"2: xc 0(256,%1),0(%1)
"
" la %1,256(%1)
"
"3: aghi %0,-256
"
" jnm 2b
"
"4: ex %0,0(%3)
"
"5: slgr %0,%0
"
"6: sacf 768
"
EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
: "+a" (size), "+a" (to), "=a" (tmp1), "=a" (tmp2)
: : "cc", "memory");
return size;
}
unsigned long __clear_user(void __user *to, unsigned long size)
{
if (static_branch_likely(&have_mvcos))
return clear_user_mvcos(to, size);
return clear_user_xc(to, size);
}
EXPORT_SYMBOL(__clear_user);
static inline unsigned long strnlen_user_srst(const char __user *src,
unsigned long size)
{
register unsigned long reg0 asm("0") = 0;
unsigned long tmp1, tmp2;
asm volatile(
" la %2,0(%1)
"
" la %3,0(%0,%1)
"
" slgr %0,%0
"
" sacf 256
"
"0: srst %3,%2
"
" jo 0b
"
" la %0,1(%3)
" /* strnlen_user results includes \0 */
" slgr %0,%1
"
"1: sacf 768
"
EX_TABLE(0b,1b)
: "+a" (size), "+a" (src), "=a" (tmp1), "=a" (tmp2)
: "d" (reg0) : "cc", "memory");
return size;
}
unsigned long __strnlen_user(const char __user *src, unsigned long size)
{
if (unlikely(!size))
return 0;
load_kernel_asce();
return strnlen_user_srst(src, size);
}
EXPORT_SYMBOL(__strnlen_user);
long __strncpy_from_user(char *dst, const char __user *src, long size)
{
size_t done, len, offset, len_str;
if (unlikely(size <= 0))
return 0;
done = 0;
do {
offset = (size_t)src & ~PAGE_MASK;
len = min(size - done, PAGE_SIZE - offset);
if (copy_from_user(dst, src, len))
return -EFAULT;
len_str = strnlen(dst, len);
done += len_str;
src += len_str;
dst += len_str;
} while ((len_str == len) && (done < size));
return done;
}
EXPORT_SYMBOL(__strncpy_from_user);
static int __init uaccess_init(void)
{
if (test_facility(27))
static_branch_enable(&have_mvcos);
return 0;
}
early_initcall(uaccess_init);
|