1/* AMD64 calling conventions checking.
2
3Copyright 2000, 2001, 2004, 2007 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library test suite.
6
7The GNU MP Library test suite is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 3 of the License,
10or (at your option) any later version.
11
12The GNU MP Library test suite is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15Public License for more details.
16
17You should have received a copy of the GNU General Public License along with
18the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
19
20#include <stdio.h>
21#include "gmp-impl.h"
22#include "tests.h"
23
24
25/* Vector if constants and register values.  We use one vector to allow access
26   via a base pointer, very beneficial for the PIC-enabled amd64call.asm.  */
27mp_limb_t calling_conventions_values[23] =
28{
29  CNST_LIMB(0x1234567887654321),	/* want_rbx */
30  CNST_LIMB(0x89ABCDEFFEDCBA98),	/* want_rbp */
31  CNST_LIMB(0xDEADBEEFBADECAFE),	/* want_r12 */
32  CNST_LIMB(0xFFEEDDCCBBAA9988),	/* want_r13 */
33  CNST_LIMB(0x0011223344556677),	/* want_r14 */
34  CNST_LIMB(0x1234432156788765),	/* want_r15 */
35
36  CNST_LIMB(0xFEEDABBACAAFBEED),	/* JUNK_RAX */
37  CNST_LIMB(0xAB78DE89FF5125BB),	/* JUNK_R10 */
38  CNST_LIMB(0x1238901890189031)		/* JUNK_R11 */
39
40  /* rest of array used for dynamic values.  */
41};
42
43/* Index starts for various regions in above vector.  */
44#define WANT	0
45#define JUNK	6
46#define SAVE	9
47#define RETADDR	15
48#define VAL	16
49#define RFLAGS	22
50
51/* values to check */
52#ifdef __cplusplus
53extern "C" {
54#endif
55struct {
56  int  control;
57  int  status;
58  int  tag;
59  int  other[4];
60} calling_conventions_fenv;
61#ifdef __cplusplus
62}
63#endif
64
65
66const char *regname[6] = {"rbx", "rbp", "r12", "r13", "r14", "r15"};
67
68#define DIR_BIT(rflags)   (((rflags) & (1<<10)) != 0)
69
70
71/* Return 1 if ok, 0 if not */
72
73int
74calling_conventions_check (void)
75{
76  const char  *header = "Violated calling conventions:\n";
77  int  ret = 1;
78  int i;
79
80#define CHECK(callreg, regstr, value)			\
81  if (callreg != value)					\
82    {							\
83      printf ("%s   %s	got 0x%016lX want 0x%016lX\n",	\
84	      header, regstr, callreg, value);		\
85      header = "";					\
86      ret = 0;						\
87    }
88
89  for (i = 0; i < 6; i++)
90    {
91      CHECK (calling_conventions_values[VAL+i], regname[i], calling_conventions_values[WANT+i]);
92    }
93
94  if (DIR_BIT (calling_conventions_values[RFLAGS]) != 0)
95    {
96      printf ("%s   rflags dir bit  got %d want 0\n",
97	      header, DIR_BIT (calling_conventions_values[RFLAGS]));
98      header = "";
99      ret = 0;
100    }
101
102  if ((calling_conventions_fenv.tag & 0xFFFF) != 0xFFFF)
103    {
104      printf ("%s   fpu tags  got 0x%X want 0xFFFF\n",
105	      header, calling_conventions_fenv.tag & 0xFFFF);
106      header = "";
107      ret = 0;
108    }
109
110  return ret;
111}
112