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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
|
/*
* Routines common to user and system emulation of load/store.
*
* Copyright (c) 2022 Linaro, Ltd.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "host/load-extract-al16-al8.h"
#include "host/store-insert-al16.h"
#ifdef CONFIG_ATOMIC64
# define HAVE_al8 true
#else
# define HAVE_al8 false
#endif
#define HAVE_al8_fast (ATOMIC_REG_SIZE >= 8)
/**
* required_atomicity:
*
* Return the lg2 bytes of atomicity required by @memop for @p.
* If the operation must be split into two operations to be
* examined separately for atomicity, return -lg2.
*/
static int required_atomicity(CPUArchState *env, uintptr_t p, MemOp memop)
{
MemOp atom = memop & MO_ATOM_MASK;
MemOp size = memop & MO_SIZE;
MemOp half = size ? size - 1 : 0;
unsigned tmp;
int atmax;
switch (atom) {
case MO_ATOM_NONE:
atmax = MO_8;
break;
case MO_ATOM_IFALIGN_PAIR:
size = half;
/* fall through */
case MO_ATOM_IFALIGN:
tmp = (1 << size) - 1;
atmax = p & tmp ? MO_8 : size;
break;
case MO_ATOM_WITHIN16:
tmp = p & 15;
atmax = (tmp + (1 << size) <= 16 ? size : MO_8);
break;
case MO_ATOM_WITHIN16_PAIR:
tmp = p & 15;
if (tmp + (1 << size) <= 16) {
atmax = size;
} else if (tmp + (1 << half) == 16) {
/*
* The pair exactly straddles the boundary.
* Both halves are naturally aligned and atomic.
*/
atmax = half;
} else {
/*
* One of the pair crosses the boundary, and is non-atomic.
* The other of the pair does not cross, and is atomic.
*/
atmax = -half;
}
break;
case MO_ATOM_SUBALIGN:
/*
* Examine the alignment of p to determine if there are subobjects
* that must be aligned. Note that we only really need ctz4() --
* any more sigificant bits are discarded by the immediately
* following comparison.
*/
tmp = ctz32(p);
atmax = MIN(size, tmp);
break;
default:
g_assert_not_reached();
}
/*
* Here we have the architectural atomicity of the operation.
* However, when executing in a serial context, we need no extra
* host atomicity in order to avoid racing. This reduction
* avoids looping with cpu_loop_exit_atomic.
*/
if (cpu_in_serial_context(env_cpu(env))) {
return MO_8;
}
return atmax;
}
/**
* load_atomic2:
* @pv: host address
*
* Atomically load 2 aligned bytes from @pv.
*/
static inline uint16_t load_atomic2(void *pv)
{
uint16_t *p = __builtin_assume_aligned(pv, 2);
return qatomic_read(p);
}
/**
* load_atomic4:
* @pv: host address
*
* Atomically load 4 aligned bytes from @pv.
*/
static inline uint32_t load_atomic4(void *pv)
{
uint32_t *p = __builtin_assume_aligned(pv, 4);
return qatomic_read(p);
}
/**
* load_atomic8:
* @pv: host address
*
* Atomically load 8 aligned bytes from @pv.
*/
static inline uint64_t load_atomic8(void *pv)
{
uint64_t *p = __builtin_assume_aligned(pv, 8);
qemu_build_assert(HAVE_al8);
return qatomic_read__nocheck(p);
}
/**
* load_atomic8_or_exit:
* @env: cpu context
* @ra: host unwind address
* @pv: host address
*
* Atomically load 8 aligned bytes from @pv.
* If this is not possible, longjmp out to restart serially.
*/
static uint64_t load_atomic8_or_exit(CPUArchState *env, uintptr_t ra, void *pv)
{
if (HAVE_al8) {
return load_atomic8(pv);
}
#ifdef CONFIG_USER_ONLY
/*
* If the page is not writable, then assume the value is immutable
* and requires no locking. This ignores the case of MAP_SHARED with
* another process, because the fallback start_exclusive solution
* provides no protection across processes.
*/
if (!page_check_range(h2g(pv), 8, PAGE_WRITE_ORG)) {
uint64_t *p = __builtin_assume_aligned(pv, 8);
return *p;
}
#endif
/* Ultimate fallback: re-execute in serial context. */
cpu_loop_exit_atomic(env_cpu(env), ra);
}
/**
* load_atomic16_or_exit:
* @env: cpu context
* @ra: host unwind address
* @pv: host address
*
* Atomically load 16 aligned bytes from @pv.
* If this is not possible, longjmp out to restart serially.
*/
static Int128 load_atomic16_or_exit(CPUArchState *env, uintptr_t ra, void *pv)
{
Int128 *p = __builtin_assume_aligned(pv, 16);
if (HAVE_ATOMIC128_RO) {
return atomic16_read_ro(p);
}
#ifdef CONFIG_USER_ONLY
/*
* We can only use cmpxchg to emulate a load if the page is writable.
* If the page is not writable, then assume the value is immutable
* and requires no locking. This ignores the case of MAP_SHARED with
* another process, because the fallback start_exclusive solution
* provides no protection across processes.
*/
if (!page_check_range(h2g(p), 16, PAGE_WRITE_ORG)) {
return *p;
}
#endif
/*
* In system mode all guest pages are writable, and for user-only
* we have just checked writability. Try cmpxchg.
*/
if (HAVE_ATOMIC128_RW) {
return atomic16_read_rw(p);
}
/* Ultimate fallback: re-execute in serial context. */
cpu_loop_exit_atomic(env_cpu(env), ra);
}
/**
* load_atom_extract_al4x2:
* @pv: host address
*
* Load 4 bytes from @p, from two sequential atomic 4-byte loads.
*/
static uint32_t load_atom_extract_al4x2(void *pv)
{
uintptr_t pi = (uintptr_t)pv;
int sh = (pi & 3) * 8;
uint32_t a, b;
pv = (void *)(pi & ~3);
a = load_atomic4(pv);
b = load_atomic4(pv + 4);
if (HOST_BIG_ENDIAN) {
return (a << sh) | (b >> (-sh & 31));
} else {
return (a >> sh) | (b << (-sh & 31));
}
}
/**
* load_atom_extract_al8x2:
* @pv: host address
*
* Load 8 bytes from @p, from two sequential atomic 8-byte loads.
*/
static uint64_t load_atom_extract_al8x2(void *pv)
{
uintptr_t pi = (uintptr_t)pv;
int sh = (pi & 7) * 8;
uint64_t a, b;
pv = (void *)(pi & ~7);
a = load_atomic8(pv);
b = load_atomic8(pv + 8);
if (HOST_BIG_ENDIAN) {
return (a << sh) | (b >> (-sh & 63));
} else {
return (a >> sh) | (b << (-sh & 63));
}
}
/**
* load_atom_extract_al8_or_exit:
* @env: cpu context
* @ra: host unwind address
* @pv: host address
* @s: object size in bytes, @s <= 4.
*
* Atomically load @s bytes from @p, when p % s != 0, and [p, p+s-1] does
* not cross an 8-byte boundary. This means that we can perform an atomic
* 8-byte load and extract.
* The value is returned in the low bits of a uint32_t.
*/
static uint32_t load_atom_extract_al8_or_exit(CPUArchState *env, uintptr_t ra,
void *pv, int s)
{
uintptr_t pi = (uintptr_t)pv;
int o = pi & 7;
int shr = (HOST_BIG_ENDIAN ? 8 - s - o : o) * 8;
pv = (void *)(pi & ~7);
return load_atomic8_or_exit(env, ra, pv) >> shr;
}
/**
* load_atom_extract_al16_or_exit:
* @env: cpu context
* @ra: host unwind address
* @p: host address
* @s: object size in bytes, @s <= 8.
*
* Atomically load @s bytes from @p, when p % 16 < 8
* and p % 16 + s > 8. I.e. does not cross a 16-byte
* boundary, but *does* cross an 8-byte boundary.
* This is the slow version, so we must have eliminated
* any faster load_atom_extract_al8_or_exit case.
*
* If this is not possible, longjmp out to restart serially.
*/
static uint64_t load_atom_extract_al16_or_exit(CPUArchState *env, uintptr_t ra,
void *pv, int s)
{
uintptr_t pi = (uintptr_t)pv;
int o = pi & 7;
int shr = (HOST_BIG_ENDIAN ? 16 - s - o : o) * 8;
Int128 r;
/*
* Note constraints above: p & 8 must be clear.
* Provoke SIGBUS if possible otherwise.
*/
pv = (void *)(pi & ~7);
r = load_atomic16_or_exit(env, ra, pv);
r = int128_urshift(r, shr);
return int128_getlo(r);
}
/**
* load_atom_4_by_2:
* @pv: host address
*
* Load 4 bytes from @pv, with two 2-byte atomic loads.
*/
static inline uint32_t load_atom_4_by_2(void *pv)
{
uint32_t a = load_atomic2(pv);
uint32_t b = load_atomic2(pv + 2);
if (HOST_BIG_ENDIAN) {
return (a << 16) | b;
} else {
return (b << 16) | a;
}
}
/**
* load_atom_8_by_2:
* @pv: host address
*
* Load 8 bytes from @pv, with four 2-byte atomic loads.
*/
static inline uint64_t load_atom_8_by_2(void *pv)
{
uint32_t a = load_atom_4_by_2(pv);
uint32_t b = load_atom_4_by_2(pv + 4);
if (HOST_BIG_ENDIAN) {
return ((uint64_t)a << 32) | b;
} else {
return ((uint64_t)b << 32) | a;
}
}
/**
* load_atom_8_by_4:
* @pv: host address
*
* Load 8 bytes from @pv, with two 4-byte atomic loads.
*/
static inline uint64_t load_atom_8_by_4(void *pv)
{
uint32_t a = load_atomic4(pv);
uint32_t b = load_atomic4(pv + 4);
if (HOST_BIG_ENDIAN) {
return ((uint64_t)a << 32) | b;
} else {
return ((uint64_t)b << 32) | a;
}
}
/**
* load_atom_8_by_8_or_4:
* @pv: host address
*
* Load 8 bytes from aligned @pv, with at least 4-byte atomicity.
*/
static inline uint64_t load_atom_8_by_8_or_4(void *pv)
{
if (HAVE_al8_fast) {
return load_atomic8(pv);
} else {
return load_atom_8_by_4(pv);
}
}
/**
* load_atom_2:
* @p: host address
* @memop: the full memory op
*
* Load 2 bytes from @p, honoring the atomicity of @memop.
*/
static uint16_t load_atom_2(CPUArchState *env, uintptr_t ra,
void *pv, MemOp memop)
{
uintptr_t pi = (uintptr_t)pv;
int atmax;
if (likely((pi & 1) == 0)) {
return load_atomic2(pv);
}
if (HAVE_ATOMIC128_RO) {
return load_atom_extract_al16_or_al8(pv, 2);
}
atmax = required_atomicity(env, pi, memop);
switch (atmax) {
case MO_8:
return lduw_he_p(pv);
case MO_16:
/* The only case remaining is MO_ATOM_WITHIN16. */
if (!HAVE_al8_fast && (pi & 3) == 1) {
/* Big or little endian, we want the middle two bytes. */
return load_atomic4(pv - 1) >> 8;
}
if ((pi & 15) != 7) {
return load_atom_extract_al8_or_exit(env, ra, pv, 2);
}
return load_atom_extract_al16_or_exit(env, ra, pv, 2);
default:
g_assert_not_reached();
}
}
/**
* load_atom_4:
* @p: host address
* @memop: the full memory op
*
* Load 4 bytes from @p, honoring the atomicity of @memop.
*/
static uint32_t load_atom_4(CPUArchState *env, uintptr_t ra,
void *pv, MemOp memop)
{
uintptr_t pi = (uintptr_t)pv;
int atmax;
if (likely((pi & 3) == 0)) {
return load_atomic4(pv);
}
if (HAVE_ATOMIC128_RO) {
return load_atom_extract_al16_or_al8(pv, 4);
}
atmax = required_atomicity(env, pi, memop);
switch (atmax) {
case MO_8:
case MO_16:
case -MO_16:
/*
* For MO_ATOM_IFALIGN, this is more atomicity than required,
* but it's trivially supported on all hosts, better than 4
* individual byte loads (when the host requires alignment),
* and overlaps with the MO_ATOM_SUBALIGN case of p % 2 == 0.
*/
return load_atom_extract_al4x2(pv);
case MO_32:
if (!(pi & 4)) {
return load_atom_extract_al8_or_exit(env, ra, pv, 4);
}
return load_atom_extract_al16_or_exit(env, ra, pv, 4);
default:
g_assert_not_reached();
}
}
/**
* load_atom_8:
* @p: host address
* @memop: the full memory op
*
* Load 8 bytes from @p, honoring the atomicity of @memop.
*/
static uint64_t load_atom_8(CPUArchState *env, uintptr_t ra,
void *pv, MemOp memop)
{
uintptr_t pi = (uintptr_t)pv;
int atmax;
/*
* If the host does not support 8-byte atomics, wait until we have
* examined the atomicity parameters below.
*/
if (HAVE_al8 && likely((pi & 7) == 0)) {
return load_atomic8(pv);
}
if (HAVE_ATOMIC128_RO) {
return load_atom_extract_al16_or_al8(pv, 8);
}
atmax = required_atomicity(env, pi, memop);
if (atmax == MO_64) {
if (!HAVE_al8 && (pi & 7) == 0) {
load_atomic8_or_exit(env, ra, pv);
}
return load_atom_extract_al16_or_exit(env, ra, pv, 8);
}
if (HAVE_al8_fast) {
return load_atom_extract_al8x2(pv);
}
switch (atmax) {
case MO_8:
return ldq_he_p(pv);
case MO_16:
return load_atom_8_by_2(pv);
case MO_32:
return load_atom_8_by_4(pv);
case -MO_32:
if (HAVE_al8) {
return load_atom_extract_al8x2(pv);
}
cpu_loop_exit_atomic(env_cpu(env), ra);
default:
g_assert_not_reached();
}
}
/**
* load_atom_16:
* @p: host address
* @memop: the full memory op
*
* Load 16 bytes from @p, honoring the atomicity of @memop.
*/
static Int128 load_atom_16(CPUArchState *env, uintptr_t ra,
void *pv, MemOp memop)
{
uintptr_t pi = (uintptr_t)pv;
int atmax;
Int128 r;
uint64_t a, b;
/*
* If the host does not support 16-byte atomics, wait until we have
* examined the atomicity parameters below.
*/
if (HAVE_ATOMIC128_RO && likely((pi & 15) == 0)) {
return atomic16_read_ro(pv);
}
atmax = required_atomicity(env, pi, memop);
switch (atmax) {
case MO_8:
memcpy(&r, pv, 16);
return r;
case MO_16:
a = load_atom_8_by_2(pv);
b = load_atom_8_by_2(pv + 8);
break;
case MO_32:
a = load_atom_8_by_4(pv);
b = load_atom_8_by_4(pv + 8);
break;
case MO_64:
if (!HAVE_al8) {
cpu_loop_exit_atomic(env_cpu(env), ra);
}
a = load_atomic8(pv);
b = load_atomic8(pv + 8);
break;
case -MO_64:
if (!HAVE_al8) {
cpu_loop_exit_atomic(env_cpu(env), ra);
}
a = load_atom_extract_al8x2(pv);
b = load_atom_extract_al8x2(pv + 8);
break;
case MO_128:
return load_atomic16_or_exit(env, ra, pv);
default:
g_assert_not_reached();
}
return int128_make128(HOST_BIG_ENDIAN ? b : a, HOST_BIG_ENDIAN ? a : b);
}
/**
* store_atomic2:
* @pv: host address
* @val: value to store
*
* Atomically store 2 aligned bytes to @pv.
*/
static inline void store_atomic2(void *pv, uint16_t val)
{
uint16_t *p = __builtin_assume_aligned(pv, 2);
qatomic_set(p, val);
}
/**
* store_atomic4:
* @pv: host address
* @val: value to store
*
* Atomically store 4 aligned bytes to @pv.
*/
static inline void store_atomic4(void *pv, uint32_t val)
{
uint32_t *p = __builtin_assume_aligned(pv, 4);
qatomic_set(p, val);
}
/**
* store_atomic8:
* @pv: host address
* @val: value to store
*
* Atomically store 8 aligned bytes to @pv.
*/
static inline void store_atomic8(void *pv, uint64_t val)
{
uint64_t *p = __builtin_assume_aligned(pv, 8);
qemu_build_assert(HAVE_al8);
qatomic_set__nocheck(p, val);
}
/**
* store_atom_4x2
*/
static inline void store_atom_4_by_2(void *pv, uint32_t val)
{
store_atomic2(pv, val >> (HOST_BIG_ENDIAN ? 16 : 0));
store_atomic2(pv + 2, val >> (HOST_BIG_ENDIAN ? 0 : 16));
}
/**
* store_atom_8_by_2
*/
static inline void store_atom_8_by_2(void *pv, uint64_t val)
{
store_atom_4_by_2(pv, val >> (HOST_BIG_ENDIAN ? 32 : 0));
store_atom_4_by_2(pv + 4, val >> (HOST_BIG_ENDIAN ? 0 : 32));
}
/**
* store_atom_8_by_4
*/
static inline void store_atom_8_by_4(void *pv, uint64_t val)
{
store_atomic4(pv, val >> (HOST_BIG_ENDIAN ? 32 : 0));
store_atomic4(pv + 4, val >> (HOST_BIG_ENDIAN ? 0 : 32));
}
/**
* store_atom_insert_al4:
* @p: host address
* @val: shifted value to store
* @msk: mask for value to store
*
* Atomically store @val to @p, masked by @msk.
*/
static void store_atom_insert_al4(uint32_t *p, uint32_t val, uint32_t msk)
{
uint32_t old, new;
p = __builtin_assume_aligned(p, 4);
old = qatomic_read(p);
do {
new = (old & ~msk) | val;
} while (!__atomic_compare_exchange_n(p, &old, new, true,
__ATOMIC_RELAXED, __ATOMIC_RELAXED));
}
/**
* store_atom_insert_al8:
* @p: host address
* @val: shifted value to store
* @msk: mask for value to store
*
* Atomically store @val to @p masked by @msk.
*/
static void store_atom_insert_al8(uint64_t *p, uint64_t val, uint64_t msk)
{
uint64_t old, new;
qemu_build_assert(HAVE_al8);
p = __builtin_assume_aligned(p, 8);
old = qatomic_read__nocheck(p);
do {
new = (old & ~msk) | val;
} while (!__atomic_compare_exchange_n(p, &old, new, true,
__ATOMIC_RELAXED, __ATOMIC_RELAXED));
}
/**
* store_bytes_leN:
* @pv: host address
* @size: number of bytes to store
* @val_le: data to store
*
* Store @size bytes at @p. The bytes to store are extracted in little-endian order
* from @val_le; return the bytes of @val_le beyond @size that have not been stored.
*/
static uint64_t store_bytes_leN(void *pv, int size, uint64_t val_le)
{
uint8_t *p = pv;
for (int i = 0; i < size; i++, val_le >>= 8) {
p[i] = val_le;
}
return val_le;
}
/**
* store_parts_leN
* @pv: host address
* @size: number of bytes to store
* @val_le: data to store
*
* As store_bytes_leN, but atomically on each aligned part.
*/
G_GNUC_UNUSED
static uint64_t store_parts_leN(void *pv, int size, uint64_t val_le)
{
do {
int n;
/* Find minimum of alignment and size */
switch (((uintptr_t)pv | size) & 7) {
case 4:
store_atomic4(pv, le32_to_cpu(val_le));
val_le >>= 32;
n = 4;
break;
case 2:
case 6:
store_atomic2(pv, le16_to_cpu(val_le));
val_le >>= 16;
n = 2;
break;
default:
*(uint8_t *)pv = val_le;
val_le >>= 8;
n = 1;
break;
case 0:
g_assert_not_reached();
}
pv += n;
size -= n;
} while (size != 0);
return val_le;
}
/**
* store_whole_le4
* @pv: host address
* @size: number of bytes to store
* @val_le: data to store
*
* As store_bytes_leN, but atomically as a whole.
* Four aligned bytes are guaranteed to cover the store.
*/
static uint64_t store_whole_le4(void *pv, int size, uint64_t val_le)
{
int sz = size * 8;
int o = (uintptr_t)pv & 3;
int sh = o * 8;
uint32_t m = MAKE_64BIT_MASK(0, sz);
uint32_t v;
if (HOST_BIG_ENDIAN) {
v = bswap32(val_le) >> sh;
m = bswap32(m) >> sh;
} else {
v = val_le << sh;
m <<= sh;
}
store_atom_insert_al4(pv - o, v, m);
return val_le >> sz;
}
/**
* store_whole_le8
* @pv: host address
* @size: number of bytes to store
* @val_le: data to store
*
* As store_bytes_leN, but atomically as a whole.
* Eight aligned bytes are guaranteed to cover the store.
*/
static uint64_t store_whole_le8(void *pv, int size, uint64_t val_le)
{
int sz = size * 8;
int o = (uintptr_t)pv & 7;
int sh = o * 8;
uint64_t m = MAKE_64BIT_MASK(0, sz);
uint64_t v;
qemu_build_assert(HAVE_al8);
if (HOST_BIG_ENDIAN) {
v = bswap64(val_le) >> sh;
m = bswap64(m) >> sh;
} else {
v = val_le << sh;
m <<= sh;
}
store_atom_insert_al8(pv - o, v, m);
return val_le >> sz;
}
/**
* store_whole_le16
* @pv: host address
* @size: number of bytes to store
* @val_le: data to store
*
* As store_bytes_leN, but atomically as a whole.
* 16 aligned bytes are guaranteed to cover the store.
*/
static uint64_t store_whole_le16(void *pv, int size, Int128 val_le)
{
int sz = size * 8;
int o = (uintptr_t)pv & 15;
int sh = o * 8;
Int128 m, v;
qemu_build_assert(HAVE_ATOMIC128_RW);
/* Like MAKE_64BIT_MASK(0, sz), but larger. */
if (sz <= 64) {
m = int128_make64(MAKE_64BIT_MASK(0, sz));
} else {
m = int128_make128(-1, MAKE_64BIT_MASK(0, sz - 64));
}
if (HOST_BIG_ENDIAN) {
v = int128_urshift(bswap128(val_le), sh);
m = int128_urshift(bswap128(m), sh);
} else {
v = int128_lshift(val_le, sh);
m = int128_lshift(m, sh);
}
store_atom_insert_al16(pv - o, v, m);
/* Unused if sz <= 64. */
return int128_gethi(val_le) >> (sz - 64);
}
/**
* store_atom_2:
* @p: host address
* @val: the value to store
* @memop: the full memory op
*
* Store 2 bytes to @p, honoring the atomicity of @memop.
*/
static void store_atom_2(CPUArchState *env, uintptr_t ra,
void *pv, MemOp memop, uint16_t val)
{
uintptr_t pi = (uintptr_t)pv;
int atmax;
if (likely((pi & 1) == 0)) {
store_atomic2(pv, val);
return;
}
atmax = required_atomicity(env, pi, memop);
if (atmax == MO_8) {
stw_he_p(pv, val);
return;
}
/*
* The only case remaining is MO_ATOM_WITHIN16.
* Big or little endian, we want the middle two bytes in each test.
*/
if ((pi & 3) == 1) {
store_atom_insert_al4(pv - 1, (uint32_t)val << 8, MAKE_64BIT_MASK(8, 16));
return;
} else if ((pi & 7) == 3) {
if (HAVE_al8) {
store_atom_insert_al8(pv - 3, (uint64_t)val << 24, MAKE_64BIT_MASK(24, 16));
return;
}
} else if ((pi & 15) == 7) {
if (HAVE_ATOMIC128_RW) {
Int128 v = int128_lshift(int128_make64(val), 56);
Int128 m = int128_lshift(int128_make64(0xffff), 56);
store_atom_insert_al16(pv - 7, v, m);
return;
}
} else {
g_assert_not_reached();
}
cpu_loop_exit_atomic(env_cpu(env), ra);
}
/**
* store_atom_4:
* @p: host address
* @val: the value to store
* @memop: the full memory op
*
* Store 4 bytes to @p, honoring the atomicity of @memop.
*/
static void store_atom_4(CPUArchState *env, uintptr_t ra,
void *pv, MemOp memop, uint32_t val)
{
uintptr_t pi = (uintptr_t)pv;
int atmax;
if (likely((pi & 3) == 0)) {
store_atomic4(pv, val);
return;
}
atmax = required_atomicity(env, pi, memop);
switch (atmax) {
case MO_8:
stl_he_p(pv, val);
return;
case MO_16:
store_atom_4_by_2(pv, val);
return;
case -MO_16:
{
uint32_t val_le = cpu_to_le32(val);
int s2 = pi & 3;
int s1 = 4 - s2;
switch (s2) {
case 1:
val_le = store_whole_le4(pv, s1, val_le);
*(uint8_t *)(pv + 3) = val_le;
break;
case 3:
*(uint8_t *)pv = val_le;
store_whole_le4(pv + 1, s2, val_le >> 8);
break;
case 0: /* aligned */
case 2: /* atmax MO_16 */
default:
g_assert_not_reached();
}
}
return;
case MO_32:
if ((pi & 7) < 4) {
if (HAVE_al8) {
store_whole_le8(pv, 4, cpu_to_le32(val));
return;
}
} else {
if (HAVE_ATOMIC128_RW) {
store_whole_le16(pv, 4, int128_make64(cpu_to_le32(val)));
return;
}
}
cpu_loop_exit_atomic(env_cpu(env), ra);
default:
g_assert_not_reached();
}
}
/**
* store_atom_8:
* @p: host address
* @val: the value to store
* @memop: the full memory op
*
* Store 8 bytes to @p, honoring the atomicity of @memop.
*/
static void store_atom_8(CPUArchState *env, uintptr_t ra,
void *pv, MemOp memop, uint64_t val)
{
uintptr_t pi = (uintptr_t)pv;
int atmax;
if (HAVE_al8 && likely((pi & 7) == 0)) {
store_atomic8(pv, val);
return;
}
atmax = required_atomicity(env, pi, memop);
switch (atmax) {
case MO_8:
stq_he_p(pv, val);
return;
case MO_16:
store_atom_8_by_2(pv, val);
return;
case MO_32:
store_atom_8_by_4(pv, val);
return;
case -MO_32:
if (HAVE_al8) {
uint64_t val_le = cpu_to_le64(val);
int s2 = pi & 7;
int s1 = 8 - s2;
switch (s2) {
case 1 ... 3:
val_le = store_whole_le8(pv, s1, val_le);
store_bytes_leN(pv + s1, s2, val_le);
break;
case 5 ... 7:
val_le = store_bytes_leN(pv, s1, val_le);
store_whole_le8(pv + s1, s2, val_le);
break;
case 0: /* aligned */
case 4: /* atmax MO_32 */
default:
g_assert_not_reached();
}
return;
}
break;
case MO_64:
if (HAVE_ATOMIC128_RW) {
store_whole_le16(pv, 8, int128_make64(cpu_to_le64(val)));
return;
}
break;
default:
g_assert_not_reached();
}
cpu_loop_exit_atomic(env_cpu(env), ra);
}
/**
* store_atom_16:
* @p: host address
* @val: the value to store
* @memop: the full memory op
*
* Store 16 bytes to @p, honoring the atomicity of @memop.
*/
static void store_atom_16(CPUArchState *env, uintptr_t ra,
void *pv, MemOp memop, Int128 val)
{
uintptr_t pi = (uintptr_t)pv;
uint64_t a, b;
int atmax;
if (HAVE_ATOMIC128_RW && likely((pi & 15) == 0)) {
atomic16_set(pv, val);
return;
}
atmax = required_atomicity(env, pi, memop);
a = HOST_BIG_ENDIAN ? int128_gethi(val) : int128_getlo(val);
b = HOST_BIG_ENDIAN ? int128_getlo(val) : int128_gethi(val);
switch (atmax) {
case MO_8:
memcpy(pv, &val, 16);
return;
case MO_16:
store_atom_8_by_2(pv, a);
store_atom_8_by_2(pv + 8, b);
return;
case MO_32:
store_atom_8_by_4(pv, a);
store_atom_8_by_4(pv + 8, b);
return;
case MO_64:
if (HAVE_al8) {
store_atomic8(pv, a);
store_atomic8(pv + 8, b);
return;
}
break;
case -MO_64:
if (HAVE_ATOMIC128_RW) {
uint64_t val_le;
int s2 = pi & 15;
int s1 = 16 - s2;
if (HOST_BIG_ENDIAN) {
val = bswap128(val);
}
switch (s2) {
case 1 ... 7:
val_le = store_whole_le16(pv, s1, val);
store_bytes_leN(pv + s1, s2, val_le);
break;
case 9 ... 15:
store_bytes_leN(pv, s1, int128_getlo(val));
val = int128_urshift(val, s1 * 8);
store_whole_le16(pv + s1, s2, val);
break;
case 0: /* aligned */
case 8: /* atmax MO_64 */
default:
g_assert_not_reached();
}
return;
}
break;
case MO_128:
if (HAVE_ATOMIC128_RW) {
atomic16_set(pv, val);
return;
}
break;
default:
g_assert_not_reached();
}
cpu_loop_exit_atomic(env_cpu(env), ra);
}
|