-
Notifications
You must be signed in to change notification settings - Fork 1
/
msync-devel.patch
4974 lines (4851 loc) · 172 KB
/
msync-devel.patch
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
From c648216fed1878ca6506c4e0d0e3e57f7a5c28e9 Mon Sep 17 00:00:00 2001
From: Marc-Aurel Zent <[email protected]>
Date: Mon, 12 Aug 2024 13:28:56 +0200
Subject: [PATCH] add msync
---
dlls/ntdll/Makefile.in | 1 +
dlls/ntdll/unix/loader.c | 2 +
dlls/ntdll/unix/msync.c | 1689 ++++++++++++++++++++++++++++++++
dlls/ntdll/unix/msync.h | 50 +
dlls/ntdll/unix/server.c | 4 +
dlls/ntdll/unix/sync.c | 69 +-
dlls/ntdll/unix/unix_private.h | 2 +
dlls/ntdll/unix/virtual.c | 2 +
include/wine/server_protocol.h | 103 +-
server/Makefile.in | 1 +
server/async.c | 2 +
server/atom.c | 1 +
server/change.c | 1 +
server/clipboard.c | 1 +
server/completion.c | 1 +
server/console.c | 33 +
server/debugger.c | 2 +
server/device.c | 26 +
server/directory.c | 2 +
server/event.c | 45 +
server/fd.c | 29 +
server/file.c | 1 +
server/file.h | 1 +
server/handle.c | 1 +
server/hook.c | 1 +
server/mailslot.c | 4 +
server/main.c | 7 +
server/mapping.c | 3 +
server/msync.c | 991 +++++++++++++++++++
server/msync.h | 36 +
server/mutex.c | 1 +
server/named_pipe.c | 5 +
server/object.h | 2 +
server/process.c | 17 +
server/process.h | 1 +
server/protocol.def | 54 +
server/queue.c | 55 ++
server/registry.c | 1 +
server/request.c | 1 +
server/request.h | 38 +
server/semaphore.c | 1 +
server/serial.c | 1 +
server/signal.c | 1 +
server/sock.c | 3 +
server/symlink.c | 1 +
server/thread.c | 40 +
server/thread.h | 2 +
server/timer.c | 19 +
server/token.c | 1 +
server/trace.c | 72 ++
server/window.c | 1 +
server/winstation.c | 2 +
52 files changed, 3428 insertions(+), 2 deletions(-)
create mode 100644 dlls/ntdll/unix/msync.c
create mode 100644 dlls/ntdll/unix/msync.h
create mode 100644 server/msync.c
create mode 100644 server/msync.h
diff --git a/dlls/ntdll/Makefile.in b/dlls/ntdll/Makefile.in
index 37bd6c86e31..89ee0286daa 100644
--- a/dlls/ntdll/Makefile.in
+++ b/dlls/ntdll/Makefile.in
@@ -49,6 +49,7 @@ SOURCES = \
unix/debug.c \
unix/env.c \
unix/file.c \
+ unix/msync.c \
unix/loader.c \
unix/loadorder.c \
unix/process.c \
diff --git a/dlls/ntdll/unix/loader.c b/dlls/ntdll/unix/loader.c
index 92f2e2eb3a3..bc9af9d34a8 100644
--- a/dlls/ntdll/unix/loader.c
+++ b/dlls/ntdll/unix/loader.c
@@ -90,6 +90,7 @@
#include "winioctl.h"
#include "winternl.h"
#include "unix_private.h"
+#include "msync.h"
#include "wine/list.h"
#include "ntsyscalls.h"
#include "wine/debug.h"
@@ -1858,6 +1859,7 @@ static void start_main_thread(void)
signal_alloc_thread( teb );
dbg_init();
startup_info_size = server_init_process();
+ msync_init();
virtual_map_user_shared_data();
init_cpu_info();
init_files();
diff --git a/dlls/ntdll/unix/msync.c b/dlls/ntdll/unix/msync.c
new file mode 100644
index 00000000000..2eef30dd0f2
--- /dev/null
+++ b/dlls/ntdll/unix/msync.c
@@ -0,0 +1,1689 @@
+/*
+ * mach semaphore-based synchronization objects
+ *
+ * Copyright (C) 2018 Zebediah Figura
+ * Copyright (C) 2023 Marc-Aurel Zent
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#if 0
+#pragma makedep unix
+#endif
+
+#include "config.h"
+
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+#ifdef HAVE_SYS_STAT_H
+# include <sys/stat.h>
+#endif
+#ifdef HAVE_SYS_SYSCALL_H
+# include <sys/syscall.h>
+#endif
+#ifdef __APPLE__
+# include <mach/mach_init.h>
+# include <mach/mach_port.h>
+# include <mach/message.h>
+# include <mach/port.h>
+# include <mach/task.h>
+# include <mach/semaphore.h>
+# include <mach/mach_error.h>
+# include <servers/bootstrap.h>
+# include <os/lock.h>
+#endif
+#include <dlfcn.h>
+#include <sched.h>
+#include <unistd.h>
+
+#include "ntstatus.h"
+#define WIN32_NO_STATUS
+#include "windef.h"
+#include "winternl.h"
+#include "wine/debug.h"
+#include "wine/server.h"
+
+#include "unix_private.h"
+#include "msync.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(msync);
+
+static LONGLONG update_timeout( ULONGLONG end )
+{
+ LARGE_INTEGER now;
+ LONGLONG timeleft;
+
+ NtQuerySystemTime( &now );
+ timeleft = end - now.QuadPart;
+ if (timeleft < 0) timeleft = 0;
+ return timeleft;
+}
+
+static inline mach_timespec_t convert_to_mach_time( LONGLONG win32_time )
+{
+ mach_timespec_t ret;
+
+ ret.tv_sec = win32_time / (ULONGLONG)TICKSPERSEC;
+ ret.tv_nsec = (win32_time % TICKSPERSEC) * 100;
+ return ret;
+}
+
+#define UL_COMPARE_AND_WAIT_SHARED 0x3
+#define ULF_WAKE_ALL 0x00000100
+#define ULF_NO_ERRNO 0x01000000
+extern int __ulock_wake( uint32_t operation, void *addr, uint64_t wake_value );
+
+typedef int (*__ulock_wait2_ptr_t)( uint32_t operation, void *addr, uint64_t value,
+ uint64_t timeout_ns, uint64_t value2 );
+static __ulock_wait2_ptr_t __ulock_wait2;
+
+/*
+ * Faster to directly do the syscall and inline everything, taken and slightly adapted
+ * from xnu/libsyscall/mach/mach_msg.c
+ */
+
+#define LIBMACH_OPTIONS64 (MACH_SEND_INTERRUPT|MACH_RCV_INTERRUPT)
+#define MACH64_SEND_MQ_CALL 0x0000000400000000ull
+
+typedef mach_msg_return_t (*mach_msg2_trap_ptr_t)( void *data, uint64_t options,
+ uint64_t msgh_bits_and_send_size, uint64_t msgh_remote_and_local_port,
+ uint64_t msgh_voucher_and_id, uint64_t desc_count_and_rcv_name,
+ uint64_t rcv_size_and_priority, uint64_t timeout );
+
+static mach_msg2_trap_ptr_t mach_msg2_trap;
+
+static inline mach_msg_return_t mach_msg2_internal( void *data, uint64_t option64, uint64_t msgh_bits_and_send_size,
+ uint64_t msgh_remote_and_local_port, uint64_t msgh_voucher_and_id, uint64_t desc_count_and_rcv_name,
+ uint64_t rcv_size_and_priority, uint64_t timeout)
+{
+ mach_msg_return_t mr;
+
+ mr = mach_msg2_trap( data, option64 & ~LIBMACH_OPTIONS64, msgh_bits_and_send_size,
+ msgh_remote_and_local_port, msgh_voucher_and_id, desc_count_and_rcv_name,
+ rcv_size_and_priority, timeout );
+
+ if (mr == MACH_MSG_SUCCESS)
+ return MACH_MSG_SUCCESS;
+
+ while (mr == MACH_SEND_INTERRUPTED)
+ mr = mach_msg2_trap( data, option64 & ~LIBMACH_OPTIONS64, msgh_bits_and_send_size,
+ msgh_remote_and_local_port, msgh_voucher_and_id, desc_count_and_rcv_name,
+ rcv_size_and_priority, timeout );
+
+ while (mr == MACH_RCV_INTERRUPTED)
+ mr = mach_msg2_trap( data, option64 & ~LIBMACH_OPTIONS64, msgh_bits_and_send_size & 0xffffffffull,
+ msgh_remote_and_local_port, msgh_voucher_and_id, desc_count_and_rcv_name,
+ rcv_size_and_priority, timeout);
+
+ return mr;
+}
+
+/* For older versions of macOS we need to provide fallback in case there is no mach_msg2... */
+extern mach_msg_return_t mach_msg_trap( mach_msg_header_t *msg, mach_msg_option_t option,
+ mach_msg_size_t send_size, mach_msg_size_t rcv_size, mach_port_name_t rcv_name, mach_msg_timeout_t timeout,
+ mach_port_name_t notify );
+
+static inline mach_msg_return_t mach_msg2( mach_msg_header_t *data, uint64_t option64,
+ mach_msg_size_t send_size, mach_msg_size_t rcv_size, mach_port_t rcv_name, uint64_t timeout,
+ uint32_t priority)
+{
+ mach_msg_base_t *base;
+ mach_msg_size_t descriptors;
+
+ if (!mach_msg2_trap)
+ return mach_msg_trap( data, (mach_msg_option_t)option64, send_size,
+ rcv_size, rcv_name, timeout, priority );
+
+ base = (mach_msg_base_t *)data;
+
+ if ((option64 & MACH_SEND_MSG) &&
+ (base->header.msgh_bits & MACH_MSGH_BITS_COMPLEX))
+ descriptors = base->body.msgh_descriptor_count;
+ else
+ descriptors = 0;
+
+#define MACH_MSG2_SHIFT_ARGS(lo, hi) ((uint64_t)hi << 32 | (uint32_t)lo)
+ return mach_msg2_internal(data, option64 | MACH64_SEND_MQ_CALL,
+ MACH_MSG2_SHIFT_ARGS(data->msgh_bits, send_size),
+ MACH_MSG2_SHIFT_ARGS(data->msgh_remote_port, data->msgh_local_port),
+ MACH_MSG2_SHIFT_ARGS(data->msgh_voucher_port, data->msgh_id),
+ MACH_MSG2_SHIFT_ARGS(descriptors, rcv_name),
+ MACH_MSG2_SHIFT_ARGS(rcv_size, priority), timeout);
+#undef MACH_MSG2_SHIFT_ARGS
+}
+
+/* this is a lot, but running out cripples performance */
+#define MAX_POOL_SEMAPHORES 1024
+#define POOL_SHRINK_THRESHOLD 30
+#define POOL_SHRINK_COUNT 10
+
+struct semaphore_memory_pool
+{
+ semaphore_t semaphores[MAX_POOL_SEMAPHORES];
+ semaphore_t *free_semaphores[MAX_POOL_SEMAPHORES];
+ unsigned int count;
+ unsigned int total;
+ os_unfair_lock lock;
+};
+
+static struct semaphore_memory_pool *pool;
+
+static void semaphore_pool_init(void)
+{
+ unsigned int i;
+
+ pool = malloc( sizeof(struct semaphore_memory_pool) );
+
+ pool->lock = OS_UNFAIR_LOCK_INIT;
+
+ for (i = 0; i < MAX_POOL_SEMAPHORES; i++)
+ {
+ pool->free_semaphores[i] = &pool->semaphores[i];
+ }
+
+ pool->count = 0;
+ pool->total = 0;
+}
+
+static inline semaphore_t *semaphore_pool_alloc(void)
+{
+ semaphore_t *new_semaphore;
+ kern_return_t kr;
+
+ os_unfair_lock_lock(&pool->lock);
+
+ if (pool->count == 0)
+ {
+ if (pool->total < MAX_POOL_SEMAPHORES)
+ {
+ TRACE("Dynamically growing semaphore pool\n");
+ kr = semaphore_create(mach_task_self(), &pool->semaphores[pool->total], SYNC_POLICY_FIFO, 0);
+ if (kr != KERN_SUCCESS)
+ ERR("Cannot create dynamic semaphore: %#x %s\n", kr, mach_error_string(kr));
+
+ new_semaphore = &pool->semaphores[pool->total];
+ pool->total++;
+
+ os_unfair_lock_unlock(&pool->lock);
+
+ return new_semaphore;
+ }
+ else
+ {
+ os_unfair_lock_unlock(&pool->lock);
+
+ WARN("Semaphore pool exhausted, consider increasing MAX_POOL_SEMAPHORES\n");
+ new_semaphore = malloc(sizeof(semaphore_t));
+ kr = semaphore_create(mach_task_self(), new_semaphore, SYNC_POLICY_FIFO, 0);
+ if (kr != KERN_SUCCESS)
+ ERR("Cannot create dynamic semaphore: %#x %s\n", kr, mach_error_string(kr));
+
+ return new_semaphore;
+ }
+ }
+
+ new_semaphore = pool->free_semaphores[pool->count - 1];
+ pool->count--;
+
+ os_unfair_lock_unlock(&pool->lock);
+
+ return new_semaphore;
+}
+
+static inline void semaphore_pool_free(semaphore_t *sem)
+{
+ int i;
+
+ os_unfair_lock_lock(&pool->lock);
+
+ if (sem < pool->semaphores || sem >= pool->semaphores + MAX_POOL_SEMAPHORES)
+ {
+ os_unfair_lock_unlock(&pool->lock);
+
+ semaphore_destroy(mach_task_self(), *sem);
+ free(sem);
+
+ return;
+ }
+
+ if (pool->count >= POOL_SHRINK_THRESHOLD)
+ {
+ TRACE("Dynamically shrinking semaphore pool\n");
+ for (i = 0; i < POOL_SHRINK_COUNT; i++)
+ {
+ semaphore_destroy(mach_task_self(), *sem);
+ pool->total--;
+ }
+ }
+ else
+ {
+ pool->free_semaphores[pool->count] = sem;
+ pool->count++;
+ }
+
+ os_unfair_lock_unlock(&pool->lock);
+}
+
+struct msync
+{
+ void *shm; /* pointer to shm section */
+ enum msync_type type;
+ unsigned int shm_idx;
+};
+
+typedef struct
+{
+ mach_msg_header_t header;
+ mach_msg_body_t body;
+ mach_msg_port_descriptor_t descriptor;
+} mach_register_message_prolog_t;
+
+typedef struct
+{
+ mach_register_message_prolog_t prolog;
+ unsigned int shm_idx[MAXIMUM_WAIT_OBJECTS + 1];
+} mach_register_message_t;
+
+typedef struct
+{
+ mach_msg_header_t header;
+ unsigned int shm_idx[MAXIMUM_WAIT_OBJECTS + 1];
+} mach_unregister_message_t;
+
+static mach_port_t server_port;
+
+static const mach_msg_bits_t msgh_bits_complex_send = MACH_MSGH_BITS_SET(
+ MACH_MSG_TYPE_COPY_SEND, 0, 0, MACH_MSGH_BITS_COMPLEX);
+
+static const mach_msg_bits_t msgh_bits_send = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND);
+
+static inline mach_msg_return_t server_register_wait( semaphore_t sem, unsigned int msgh_id,
+ struct msync **wait_objs, const int count )
+{
+ int i, is_mutex;
+ mach_msg_return_t mr;
+ __thread static mach_register_message_t message;
+
+ message.prolog.header.msgh_remote_port = server_port;
+ message.prolog.header.msgh_bits = msgh_bits_complex_send;
+ message.prolog.header.msgh_id = msgh_id;
+
+ message.prolog.body.msgh_descriptor_count = 1;
+
+ message.prolog.descriptor.name = sem;
+ message.prolog.descriptor.disposition = MACH_MSG_TYPE_COPY_SEND;
+ message.prolog.descriptor.type = MACH_MSG_PORT_DESCRIPTOR;
+
+ for (i = 0; i < count; i++)
+ {
+ is_mutex = wait_objs[i]->type == MSYNC_MUTEX ? 1 : 0;
+ message.shm_idx[i] = wait_objs[i]->shm_idx | (is_mutex << 28);
+ __atomic_add_fetch( (int *)(wait_objs[i]->shm) + 3, 1, __ATOMIC_SEQ_CST);
+ }
+
+ message.prolog.header.msgh_size = sizeof(mach_register_message_prolog_t) +
+ count * sizeof(unsigned int);
+
+ mr = mach_msg2( (mach_msg_header_t *)&message, MACH_SEND_MSG, message.prolog.header.msgh_size,
+ 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, 0 );
+
+ if (mr != MACH_MSG_SUCCESS)
+ ERR("Failed to send server register wait: %#x\n", mr);
+
+ return mr;
+}
+
+static inline void server_remove_wait( unsigned int msgh_id, struct msync **wait_objs, const int count )
+{
+ int i;
+ mach_msg_return_t mr;
+ __thread static mach_unregister_message_t message;
+
+ message.header.msgh_remote_port = server_port;
+ message.header.msgh_bits = msgh_bits_send;
+ message.header.msgh_id = msgh_id;
+
+ for (i = 0; i < count; i++)
+ {
+ int old_val, new_val;
+ do
+ {
+ old_val = __atomic_load_n( (int *)(wait_objs[i]->shm) + 3, __ATOMIC_SEQ_CST );
+ if (old_val <= 0) break;
+ new_val = old_val - 1;
+ } while (!__atomic_compare_exchange_n( (int *)(wait_objs[i]->shm) + 3, &old_val,
+ new_val, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST ));
+
+ message.shm_idx[i] = wait_objs[i]->shm_idx;
+ }
+
+ message.header.msgh_size = sizeof(mach_msg_header_t) +
+ count * sizeof(unsigned int);
+
+ mr = mach_msg2( (mach_msg_header_t *)&message, MACH_SEND_MSG, message.header.msgh_size,
+ 0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, 0 );
+
+ if (mr != MACH_MSG_SUCCESS)
+ ERR("Failed to send server remove wait: %#x\n", mr);
+}
+
+static NTSTATUS destroyed_wait( ULONGLONG *end )
+{
+ if (end)
+ {
+ usleep( update_timeout( *end ) / 10 );
+ return STATUS_TIMEOUT;
+ }
+ pause();
+ return STATUS_PENDING;
+}
+
+static inline int is_destroyed( struct msync **objs, int count)
+{
+ int i;
+
+ for (i = 0; i < count; i++)
+ if (__atomic_load_n( (int *)objs[i]->shm + 2, __ATOMIC_RELAXED ))
+ return 0;
+
+ return 1;
+}
+
+static inline NTSTATUS msync_wait_single( struct msync *wait_obj,
+ ULONGLONG *end, int tid )
+{
+ int ret, val = 0;
+ void *addr = wait_obj->shm;
+ ULONGLONG ns_timeleft = 0;
+
+ do
+ {
+ if (wait_obj->type == MSYNC_MUTEX)
+ {
+ val = __atomic_load_n( (int *)addr, __ATOMIC_ACQUIRE );
+ if (!val || val == ~0)
+ val = tid;
+ }
+
+ if (__atomic_load_n( (int *)addr, __ATOMIC_ACQUIRE ) != val)
+ return STATUS_PENDING;
+
+ if (end)
+ {
+ ns_timeleft = update_timeout( *end ) * 100;
+ if (!ns_timeleft) return STATUS_TIMEOUT;
+ }
+ ret = __ulock_wait2( UL_COMPARE_AND_WAIT_SHARED | ULF_NO_ERRNO, addr, val, ns_timeleft, 0 );
+ } while (ret == -EINTR);
+
+ if (ret == -ETIMEDOUT)
+ return STATUS_TIMEOUT;
+
+ if (is_destroyed( &wait_obj, 1 ))
+ return destroyed_wait( end );
+
+ return STATUS_SUCCESS;
+}
+
+static inline int resize_wait_objs( struct msync **wait_objs, struct msync **objs, int count )
+{
+ int read_index, write_index = 0;
+
+ for (read_index = 0; read_index < count; read_index++)
+ {
+ if (wait_objs[read_index] &&
+ __atomic_load_n( (int *)wait_objs[read_index]->shm + 2, __ATOMIC_RELAXED ))
+ {
+ objs[write_index] = wait_objs[read_index];
+ write_index++;
+ }
+ }
+
+ return write_index;
+}
+
+static inline int check_shm_contention( struct msync **wait_objs,
+ int count, int tid )
+{
+ int i, val;
+
+ for (i = 0; i < count; i++)
+ {
+ val = __atomic_load_n((int *)wait_objs[i]->shm, __ATOMIC_SEQ_CST);
+ if (wait_objs[i]->type == MSYNC_MUTEX)
+ {
+ if (val == 0 || val == ~0 || val == tid) return 1;
+ }
+ else
+ {
+ if (val != 0) return 1;
+ }
+ }
+ return 0;
+}
+
+static NTSTATUS msync_wait_multiple( struct msync **wait_objs,
+ int count, ULONGLONG *end, int tid )
+{
+ semaphore_t *sem;
+ kern_return_t kr;
+ mach_msg_return_t mr;
+ unsigned int msgh_id;
+ __thread static struct msync *objs[MAXIMUM_WAIT_OBJECTS + 1];
+
+ count = resize_wait_objs( wait_objs, objs, count );
+
+ if (count == 1 && __ulock_wait2) return msync_wait_single( objs[0], end, tid );
+ if (!count) return destroyed_wait( end );
+
+ if (check_shm_contention( objs, count, tid ))
+ return STATUS_PENDING;
+
+ sem = semaphore_pool_alloc();
+ msgh_id = (tid << 8) | count;
+ mr = server_register_wait( *sem, msgh_id, objs, count );
+
+ if (mr != MACH_MSG_SUCCESS)
+ {
+ /* The os failed to send the mach message, which is either because
+ * we lost the wineserver process or the semaphore from semaphore_pool_alloc
+ * is not valid...
+ * To avoid pooling in a dead port, recreate it here.
+ * Worst case, this will behave effectively like a spinlock. */
+ semaphore_destroy( mach_task_self(), *sem) ;
+ kr = semaphore_create( mach_task_self(), sem, SYNC_POLICY_FIFO, 0 );
+
+ if (kr != KERN_SUCCESS)
+ {
+ ERR("Cannot create semaphore: %#x %s\n", kr, mach_error_string(kr));
+ semaphore_pool_free( sem );
+ return STATUS_PENDING;
+ }
+
+ mr = server_register_wait( *sem, msgh_id, objs, count );
+
+ if (mr != MACH_MSG_SUCCESS)
+ {
+ semaphore_pool_free( sem );
+ return STATUS_PENDING;
+ }
+ }
+
+ do
+ {
+ if (end)
+ kr = semaphore_timedwait( *sem,
+ convert_to_mach_time( update_timeout( *end ) ) );
+ else
+ kr = semaphore_wait( *sem );
+ } while (kr == KERN_ABORTED);
+
+ semaphore_pool_free( sem );
+
+ if (is_destroyed( objs, count ))
+ return destroyed_wait( end );
+
+ server_remove_wait( msgh_id, objs, count );
+
+ switch (kr) {
+ case KERN_SUCCESS:
+ return STATUS_SUCCESS;
+ case KERN_OPERATION_TIMED_OUT:
+ if (check_shm_contention( objs, count, tid ))
+ return STATUS_PENDING;
+ return STATUS_TIMEOUT;
+ case KERN_TERMINATED:
+ return destroyed_wait( end );
+ default:
+ ERR("Unexpected kernel return code: %#x %s\n", kr, mach_error_string( kr ));
+ return STATUS_PENDING;
+ }
+}
+
+int do_msync(void)
+{
+#ifdef __APPLE__
+ static int do_msync_cached = -1;
+
+ if (do_msync_cached == -1)
+ do_msync_cached = getenv("WINEMSYNC") && atoi(getenv("WINEMSYNC"));
+
+ return do_msync_cached;
+#else
+ static int once;
+ if (!once++)
+ FIXME("mach semaphores not supported on this platform.\n");
+ return 0;
+#endif
+}
+
+struct semaphore
+{
+ int count;
+ int max;
+};
+C_ASSERT(sizeof(struct semaphore) == 8);
+
+struct event
+{
+ int signaled;
+ int unused;
+};
+C_ASSERT(sizeof(struct event) == 8);
+
+struct mutex
+{
+ int tid;
+ int count; /* recursion count */
+};
+C_ASSERT(sizeof(struct mutex) == 8);
+
+static char shm_name[29];
+static int shm_fd;
+static void **shm_addrs;
+static int shm_addrs_size; /* length of the allocated shm_addrs array */
+static long pagesize;
+
+static os_unfair_lock shm_addrs_lock = OS_UNFAIR_LOCK_INIT;
+
+static void *get_shm( unsigned int idx )
+{
+ int entry = (idx * 16) / pagesize;
+ int offset = (idx * 16) % pagesize;
+ void *ret;
+
+ os_unfair_lock_lock( &shm_addrs_lock );
+
+ if (entry >= shm_addrs_size)
+ {
+ int new_size = max(shm_addrs_size * 2, entry + 1);
+
+ if (!(shm_addrs = realloc( shm_addrs, new_size * sizeof(shm_addrs[0]) )))
+ ERR("Failed to grow shm_addrs array to size %d.\n", shm_addrs_size);
+ memset( shm_addrs + shm_addrs_size, 0, (new_size - shm_addrs_size) * sizeof(shm_addrs[0]) );
+ shm_addrs_size = new_size;
+ }
+
+ if (!shm_addrs[entry])
+ {
+ void *addr = mmap( NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, entry * pagesize );
+ if (addr == (void *)-1)
+ ERR("Failed to map page %d (offset %#lx).\n", entry, entry * pagesize);
+
+ TRACE("Mapping page %d at %p.\n", entry, addr);
+
+ if (__sync_val_compare_and_swap( &shm_addrs[entry], 0, addr ))
+ munmap( addr, pagesize ); /* someone beat us to it */
+ }
+
+ ret = (void *)((unsigned long)shm_addrs[entry] + offset);
+
+ os_unfair_lock_unlock( &shm_addrs_lock );
+
+ return ret;
+}
+
+/* We'd like lookup to be fast. To that end, we use a static list indexed by handle.
+ * This is copied and adapted from the fd cache code. */
+
+#define MSYNC_LIST_BLOCK_SIZE (65536 / sizeof(struct msync))
+#define MSYNC_LIST_ENTRIES 256
+
+static struct msync *msync_list[MSYNC_LIST_ENTRIES];
+static struct msync msync_list_initial_block[MSYNC_LIST_BLOCK_SIZE];
+
+static inline UINT_PTR handle_to_index( HANDLE handle, UINT_PTR *entry )
+{
+ UINT_PTR idx = (((UINT_PTR)handle) >> 2) - 1;
+ *entry = idx / MSYNC_LIST_BLOCK_SIZE;
+ return idx % MSYNC_LIST_BLOCK_SIZE;
+}
+
+static struct msync *add_to_list( HANDLE handle, enum msync_type type, unsigned int shm_idx )
+{
+ UINT_PTR entry, idx = handle_to_index( handle, &entry );
+ void *shm = get_shm( shm_idx );
+
+ if (entry >= MSYNC_LIST_ENTRIES)
+ {
+ FIXME( "too many allocated handles, not caching %p\n", handle );
+ return FALSE;
+ }
+
+ if (!msync_list[entry]) /* do we need to allocate a new block of entries? */
+ {
+ if (!entry) msync_list[0] = msync_list_initial_block;
+ else
+ {
+ void *ptr = anon_mmap_alloc( MSYNC_LIST_BLOCK_SIZE * sizeof(struct msync),
+ PROT_READ | PROT_WRITE );
+ if (ptr == MAP_FAILED) return FALSE;
+ msync_list[entry] = ptr;
+ }
+ }
+
+ if (!__sync_val_compare_and_swap((int *)&msync_list[entry][idx].type, 0, type ))
+ {
+ msync_list[entry][idx].shm = shm;
+ msync_list[entry][idx].shm_idx = shm_idx;
+ }
+
+ return &msync_list[entry][idx];
+}
+
+static struct msync *get_cached_object( HANDLE handle )
+{
+ UINT_PTR entry, idx = handle_to_index( handle, &entry );
+
+ if (entry >= MSYNC_LIST_ENTRIES || !msync_list[entry]) return NULL;
+ if (!msync_list[entry][idx].type) return NULL;
+
+ return &msync_list[entry][idx];
+}
+
+/* Gets an object. This is either a proper msync object (i.e. an event,
+ * semaphore, etc. created using create_msync) or a generic synchronizable
+ * server-side object which the server will signal (e.g. a process, thread,
+ * message queue, etc.) */
+static NTSTATUS get_object( HANDLE handle, struct msync **obj )
+{
+ NTSTATUS ret = STATUS_SUCCESS;
+ unsigned int shm_idx = 0;
+ enum msync_type type;
+
+ if ((*obj = get_cached_object( handle ))) return STATUS_SUCCESS;
+
+ if ((INT_PTR)handle < 0)
+ {
+ /* We can deal with pseudo-handles, but it's just easier this way */
+ return STATUS_NOT_IMPLEMENTED;
+ }
+
+ /* We need to try grabbing it from the server. */
+ SERVER_START_REQ( get_msync_idx )
+ {
+ req->handle = wine_server_obj_handle( handle );
+ if (!(ret = wine_server_call( req )))
+ {
+ shm_idx = reply->shm_idx;
+ type = reply->type;
+ }
+ }
+ SERVER_END_REQ;
+
+ if (ret)
+ {
+ WARN("Failed to retrieve shm index for handle %p, status %#x.\n", handle, ret);
+ *obj = NULL;
+ return ret;
+ }
+
+ TRACE("Got shm index %d for handle %p.\n", shm_idx, handle);
+ *obj = add_to_list( handle, type, shm_idx );
+ return ret;
+}
+
+NTSTATUS msync_close( HANDLE handle )
+{
+ UINT_PTR entry, idx = handle_to_index( handle, &entry );
+
+ TRACE("%p.\n", handle);
+
+ if (entry < MSYNC_LIST_ENTRIES && msync_list[entry])
+ {
+ if (__atomic_exchange_n( &msync_list[entry][idx].type, 0, __ATOMIC_SEQ_CST ))
+ return STATUS_SUCCESS;
+ }
+
+ return STATUS_INVALID_HANDLE;
+}
+
+static NTSTATUS create_msync( enum msync_type type, HANDLE *handle,
+ ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr, int low, int high )
+{
+ NTSTATUS ret;
+ data_size_t len;
+ struct object_attributes *objattr;
+ unsigned int shm_idx;
+
+ if ((ret = alloc_object_attributes( attr, &objattr, &len ))) return ret;
+
+ SERVER_START_REQ( create_msync )
+ {
+ req->access = access;
+ req->low = low;
+ req->high = high;
+ req->type = type;
+ wine_server_add_data( req, objattr, len );
+ ret = wine_server_call( req );
+ if (!ret || ret == STATUS_OBJECT_NAME_EXISTS)
+ {
+ *handle = wine_server_ptr_handle( reply->handle );
+ shm_idx = reply->shm_idx;
+ type = reply->type;
+ }
+ }
+ SERVER_END_REQ;
+
+ if (!ret || ret == STATUS_OBJECT_NAME_EXISTS)
+ {
+ add_to_list( *handle, type, shm_idx );
+ TRACE("-> handle %p, shm index %d.\n", *handle, shm_idx);
+ }
+
+ free( objattr );
+ return ret;
+}
+
+static NTSTATUS open_msync( enum msync_type type, HANDLE *handle,
+ ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
+{
+ NTSTATUS ret;
+ unsigned int shm_idx;
+
+ SERVER_START_REQ( open_msync )
+ {
+ req->access = access;
+ req->attributes = attr->Attributes;
+ req->rootdir = wine_server_obj_handle( attr->RootDirectory );
+ req->type = type;
+ if (attr->ObjectName)
+ wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
+ if (!(ret = wine_server_call( req )))
+ {
+ *handle = wine_server_ptr_handle( reply->handle );
+ type = reply->type;
+ shm_idx = reply->shm_idx;
+ }
+ }
+ SERVER_END_REQ;
+
+ if (!ret)
+ {
+ add_to_list( *handle, type, shm_idx );
+ TRACE("-> handle %p, shm index %u.\n", *handle, shm_idx);
+ }
+ return ret;
+}
+
+void msync_init(void)
+{
+ struct stat st;
+ mach_port_t bootstrap_port;
+ void *dlhandle = dlopen( NULL, RTLD_NOW );
+
+ if (!do_msync())
+ {
+ /* make sure the server isn't running with WINEMSYNC */
+ HANDLE handle;
+ NTSTATUS ret;
+
+ ret = create_msync( 0, &handle, 0, NULL, 0, 0 );
+ if (ret != STATUS_NOT_IMPLEMENTED)
+ {
+ ERR("Server is running with WINEMSYNC but this process is not, please enable WINEMSYNC or restart wineserver.\n");
+ exit(1);
+ }
+
+ dlclose( dlhandle );
+ return;
+ }
+
+ if (stat( config_dir, &st ) == -1)
+ ERR("Cannot stat %s\n", config_dir);
+
+ if (st.st_ino != (unsigned long)st.st_ino)
+ sprintf( shm_name, "/wine-%lx%08lx-msync", (unsigned long)((unsigned long long)st.st_ino >> 32), (unsigned long)st.st_ino );
+ else
+ sprintf( shm_name, "/wine-%lx-msync", (unsigned long)st.st_ino );
+
+ if ((shm_fd = shm_open( shm_name, O_RDWR, 0644 )) == -1)
+ {
+ /* probably the server isn't running with WINEMSYNC, tell the user and bail */
+ if (errno == ENOENT)
+ ERR("Failed to open msync shared memory file; make sure no stale wineserver instances are running without WINEMSYNC.\n");
+ else
+ ERR("Failed to initialize shared memory: %s\n", strerror( errno ));
+ exit(1);
+ }
+
+ pagesize = sysconf( _SC_PAGESIZE );
+
+ shm_addrs = calloc( 128, sizeof(shm_addrs[0]) );
+ shm_addrs_size = 128;
+
+ semaphore_pool_init();
+
+ __ulock_wait2 = (__ulock_wait2_ptr_t)dlsym( dlhandle, "__ulock_wait2" );
+ if (!__ulock_wait2)
+ WARN("__ulock_wait2 not available, performance will be lower\n");
+
+ /* Bootstrap mach wineserver communication */
+
+ mach_msg2_trap = (mach_msg2_trap_ptr_t)dlsym( dlhandle, "mach_msg2_trap" );
+ if (!mach_msg2_trap)
+ WARN("Using mach_msg_overwrite instead of mach_msg2\n");
+ dlclose( dlhandle );
+
+ if (task_get_special_port(mach_task_self(), TASK_BOOTSTRAP_PORT, &bootstrap_port) != KERN_SUCCESS)
+ {
+ ERR("Failed task_get_special_port\n");
+ exit(1);
+ }
+
+ if (bootstrap_look_up(bootstrap_port, shm_name + 1, &server_port) != KERN_SUCCESS)
+ {
+ ERR("Failed bootstrap_look_up for %s\n", shm_name + 1);
+ exit(1);
+ }
+}
+