summaryrefslogtreecommitdiff
path: root/libitm/memcpy.cc
blob: 9919e6a9afaceb02de99742576fa01c9edff454f (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
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
/* Copyright (C) 2008, 2009, 2011 Free Software Foundation, Inc.
   Contributed by Richard Henderson <rth@redhat.com>.

   This file is part of the GNU Transactional Memory Library (libitm).

   Libitm is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   Libitm 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 General Public License for
   more details.

   Under Section 7 of GPL version 3, you are granted additional
   permissions described in the GCC Runtime Library Exception, version
   3.1, as published by the Free Software Foundation.

   You should have received a copy of the GNU General Public License and
   a copy of the GCC Runtime Library Exception along with this program;
   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
   <http://www.gnu.org/licenses/>.  */

#include "libitm_i.h"

using namespace GTM;

static void
do_memcpy (uintptr_t idst, uintptr_t isrc, size_t size,
	   abi_dispatch::lock_type W, abi_dispatch::lock_type R)
{
  abi_dispatch *disp = abi_disp();
  // The position in the destination cacheline where *IDST starts.
  uintptr_t dofs = idst & (CACHELINE_SIZE - 1);
  // The position in the source cacheline where *ISRC starts.
  uintptr_t sofs = isrc & (CACHELINE_SIZE - 1);
  const gtm_cacheline *src
    = reinterpret_cast<const gtm_cacheline *>(isrc & -CACHELINE_SIZE);
  gtm_cacheline *dst
    = reinterpret_cast<gtm_cacheline *>(idst & -CACHELINE_SIZE);
  const gtm_cacheline *sline;
  abi_dispatch::mask_pair dpair;

  if (size == 0)
    return;

  // If both SRC and DST data start at the same position in the cachelines,
  // we can easily copy the data in tandem, cacheline by cacheline...
  if (dofs == sofs)
    {
      // We copy the data in three stages:

      // (a) Copy stray bytes at the beginning that are smaller than a
      // cacheline.
      if (sofs != 0)
	{
	  size_t sleft = CACHELINE_SIZE - sofs;
	  size_t min = (size <= sleft ? size : sleft);

	  dpair = disp->write_lock(dst, W);
	  sline = disp->read_lock(src, R);
	  *dpair.mask |= (((gtm_cacheline_mask)1 << min) - 1) << sofs;
	  memcpy (&dpair.line->b[sofs], &sline->b[sofs], min);
	  dst++;
	  src++;
	  size -= min;
	}

      // (b) Copy subsequent cacheline sized chunks.
      while (size >= CACHELINE_SIZE)
	{
	  dpair = disp->write_lock(dst, W);
	  sline = disp->read_lock(src, R);
	  *dpair.mask = -1;
	  *dpair.line = *sline;
	  dst++;
	  src++;
	  size -= CACHELINE_SIZE;
	}

      // (c) Copy anything left over.
      if (size != 0)
	{
	  dpair = disp->write_lock(dst, W);
	  sline = disp->read_lock(src, R);
	  *dpair.mask |= ((gtm_cacheline_mask)1 << size) - 1;
	  memcpy (dpair.line, sline, size);
	}
    }
  // ... otherwise, we must copy the data in disparate hunks using
  // temporary storage.
  else
    {
      gtm_cacheline c;
      size_t sleft = CACHELINE_SIZE - sofs;

      sline = disp->read_lock(src, R);

      // As above, we copy the data in three stages:

      // (a) Copy stray bytes at the beginning that are smaller than a
      // cacheline.
      if (dofs != 0)
	{
	  size_t dleft = CACHELINE_SIZE - dofs;
	  size_t min = (size <= dleft ? size : dleft);

	  dpair = disp->write_lock(dst, W);
	  *dpair.mask |= (((gtm_cacheline_mask)1 << min) - 1) << dofs;

	  // If what's left in the source cacheline will fit in the
	  // rest of the destination cacheline, straight up copy it.
	  if (min <= sleft)
	    {
	      memcpy (&dpair.line->b[dofs], &sline->b[sofs], min);
	      sofs += min;
	    }
	  // Otherwise, we need more bits from the source cacheline
	  // that are available.  Piece together what we need from
	  // contiguous (source) cachelines, into temp space, and copy
	  // it over.
	  else
	    {
	      memcpy (&c, &sline->b[sofs], sleft);
	      sline = disp->read_lock(++src, R);
	      sofs = min - sleft;
	      memcpy (&c.b[sleft], sline, sofs);
	      memcpy (&dpair.line->b[dofs], &c, min);
	    }
	  sleft = CACHELINE_SIZE - sofs;

	  dst++;
	  size -= min;
	}

      // (b) Copy subsequent cacheline sized chunks.
      while (size >= CACHELINE_SIZE)
	{
	  // We have a full (destination) cacheline where to put the
	  // data, but to get to the corresponding cacheline sized
	  // chunk in the source, we have to piece together two
	  // contiguous source cachelines.

	  memcpy (&c, &sline->b[sofs], sleft);
	  sline = disp->read_lock(++src, R);
	  memcpy (&c.b[sleft], sline, sofs);

	  dpair = disp->write_lock(dst, W);
	  *dpair.mask = -1;
	  *dpair.line = c;

	  dst++;
	  size -= CACHELINE_SIZE;
	}

      // (c) Copy anything left over.
      if (size != 0)
	{
	  dpair = disp->write_lock(dst, W);
	  *dpair.mask |= ((gtm_cacheline_mask)1 << size) - 1;
	  // If what's left to copy is entirely in the remaining
	  // source cacheline, do it.
	  if (size <= sleft)
	    memcpy (dpair.line, &sline->b[sofs], size);
	  // Otherwise, piece together the remaining bits, and copy.
	  else
	    {
	      memcpy (&c, &sline->b[sofs], sleft);
	      sline = disp->read_lock(++src, R);
	      memcpy (&c.b[sleft], sline, size - sleft);
	      memcpy (dpair.line, &c, size);
	    }
	}
    }
}

static void
do_memmove (uintptr_t idst, uintptr_t isrc, size_t size,
	    abi_dispatch::lock_type W, abi_dispatch::lock_type R)
{
  abi_dispatch *disp = abi_disp();
  uintptr_t dleft, sleft, sofs, dofs;
  const gtm_cacheline *sline;
  abi_dispatch::mask_pair dpair;

  if (size == 0)
    return;

  /* The co-aligned memmove below doesn't work for DST == SRC, so filter
     that out.  It's tempting to just return here, as this is a no-op move.
     However, our caller has the right to expect the locks to be acquired
     as advertized.  */
  if (__builtin_expect (idst == isrc, 0))
    {
      /* If the write lock is already acquired, nothing to do.  */
      if (W == abi_dispatch::WaW)
	return;
      /* If the destination is protected, acquire a write lock.  */
      if (W != abi_dispatch::NOLOCK)
	R = abi_dispatch::RfW;
      /* Notice serial mode, where we don't acquire locks at all.  */
      if (R == abi_dispatch::NOLOCK)
	return;

      idst = isrc + size;
      for (isrc &= -CACHELINE_SIZE; isrc < idst; isrc += CACHELINE_SIZE)
	disp->read_lock(reinterpret_cast<const gtm_cacheline *>(isrc), R);
      return;
    }

  /* Fall back to memcpy if the implementation above can handle it.  */
  if (idst < isrc || isrc + size <= idst)
    {
      do_memcpy (idst, isrc, size, W, R);
      return;
    }

  /* What remains requires a backward copy from the end of the blocks.  */
  idst += size;
  isrc += size;
  dofs = idst & (CACHELINE_SIZE - 1);
  sofs = isrc & (CACHELINE_SIZE - 1);
  dleft = CACHELINE_SIZE - dofs;
  sleft = CACHELINE_SIZE - sofs;

  gtm_cacheline *dst
    = reinterpret_cast<gtm_cacheline *>(idst & -CACHELINE_SIZE);
  const gtm_cacheline *src
    = reinterpret_cast<const gtm_cacheline *>(isrc & -CACHELINE_SIZE);
  if (dofs == 0)
    dst--;
  if (sofs == 0)
    src--;

  if (dofs == sofs)
    {
      /* Since DST and SRC are co-aligned, and we didn't use the memcpy
	 optimization above, that implies that SIZE > CACHELINE_SIZE.  */
      if (sofs != 0)
	{
	  dpair = disp->write_lock(dst, W);
	  sline = disp->read_lock(src, R);
	  *dpair.mask |= ((gtm_cacheline_mask)1 << sleft) - 1;
	  memcpy (dpair.line, sline, sleft);
	  dst--;
	  src--;
	  size -= sleft;
	}

      while (size >= CACHELINE_SIZE)
	{
	  dpair = disp->write_lock(dst, W);
	  sline = disp->read_lock(src, R);
	  *dpair.mask = -1;
	  *dpair.line = *sline;
	  dst--;
	  src--;
	  size -= CACHELINE_SIZE;
	}

      if (size != 0)
	{
	  size_t ofs = CACHELINE_SIZE - size;
	  dpair = disp->write_lock(dst, W);
	  sline = disp->read_lock(src, R);
	  *dpair.mask |= (((gtm_cacheline_mask)1 << size) - 1) << ofs;
	  memcpy (&dpair.line->b[ofs], &sline->b[ofs], size);
	}
    }
  else
    {
      gtm_cacheline c;

      sline = disp->read_lock(src, R);
      if (dofs != 0)
	{
	  size_t min = (size <= dofs ? size : dofs);

	  if (min <= sofs)
	    {
	      sofs -= min;
	      memcpy (&c, &sline->b[sofs], min);
	    }
	  else
	    {
	      size_t min_ofs = min - sofs;
	      memcpy (&c.b[min_ofs], sline, sofs);
	      sline = disp->read_lock(--src, R);
	      sofs = CACHELINE_SIZE - min_ofs;
	      memcpy (&c, &sline->b[sofs], min_ofs);
	    }

	  dofs = dleft - min;
	  dpair = disp->write_lock(dst, W);
	  *dpair.mask |= (((gtm_cacheline_mask)1 << min) - 1) << dofs;
	  memcpy (&dpair.line->b[dofs], &c, min);

	  sleft = CACHELINE_SIZE - sofs;
	  dst--;
	  size -= min;
	}

      while (size >= CACHELINE_SIZE)
	{
	  memcpy (&c.b[sleft], sline, sofs);
	  sline = disp->read_lock(--src, R);
	  memcpy (&c, &sline->b[sofs], sleft);

	  dpair = disp->write_lock(dst, W);
	  *dpair.mask = -1;
	  *dpair.line = c;

	  dst--;
	  size -= CACHELINE_SIZE;
	}

      if (size != 0)
	{
	  dofs = CACHELINE_SIZE - size;

	  memcpy (&c.b[sleft], sline, sofs);
	  if (sleft > dofs)
	    {
	      sline = disp->read_lock(--src, R);
	      memcpy (&c, &sline->b[sofs], sleft);
	    }

	  dpair = disp->write_lock(dst, W);
	  *dpair.mask |= (gtm_cacheline_mask)-1 << dofs;
	  memcpy (&dpair.line->b[dofs], &c.b[dofs], size);
	}
    }
}

#define ITM_MEM_DEF(NAME, READ, WRITE) \
void ITM_REGPARM _ITM_memcpy##NAME(void *dst, const void *src, size_t size)  \
{									     \
  do_memcpy ((uintptr_t)dst, (uintptr_t)src, size,			     \
	     abi_dispatch::WRITE, abi_dispatch::READ);			     \
}									     \
void ITM_REGPARM _ITM_memmove##NAME(void *dst, const void *src, size_t size) \
{									     \
  do_memmove ((uintptr_t)dst, (uintptr_t)src, size,			     \
	      abi_dispatch::WRITE, abi_dispatch::READ);			     \
}

ITM_MEM_DEF(RnWt,	NOLOCK,		W)
ITM_MEM_DEF(RnWtaR,	NOLOCK,		WaR)
ITM_MEM_DEF(RnWtaW,	NOLOCK,		WaW)

ITM_MEM_DEF(RtWn,	R,		NOLOCK)
ITM_MEM_DEF(RtWt,	R,		W)
ITM_MEM_DEF(RtWtaR,	R,		WaR)
ITM_MEM_DEF(RtWtaW,	R,		WaW)

ITM_MEM_DEF(RtaRWn,	RaR,		NOLOCK)
ITM_MEM_DEF(RtaRWt,	RaR,		W)
ITM_MEM_DEF(RtaRWtaR,	RaR,		WaR)
ITM_MEM_DEF(RtaRWtaW,	RaR,		WaW)

ITM_MEM_DEF(RtaWWn,	RaW,		NOLOCK)
ITM_MEM_DEF(RtaWWt,	RaW,		W)
ITM_MEM_DEF(RtaWWtaR,	RaW,		WaR)
ITM_MEM_DEF(RtaWWtaW,	RaW,		WaW)