1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/cpu.h"
6#include "base/logging.h"
7#include "base/strings/string_util.h"
8#if defined(OS_WIN)
9#include "base/win/windows_version.h"
10#endif
11
12namespace nacl {
13
14const char* GetSandboxArch() {
15#if defined(ARCH_CPU_ARM_FAMILY)
16  return "arm";
17#elif defined(ARCH_CPU_MIPS_FAMILY)
18  return "mips32";
19#elif defined(ARCH_CPU_X86_FAMILY)
20
21#if defined(OS_WIN)
22  // We have to check the host architecture on Windows.
23  // See sandbox_isa.h for an explanation why.
24  if (base::win::OSInfo::GetInstance()->architecture() ==
25      base::win::OSInfo::X64_ARCHITECTURE)
26    return "x86-64";
27  else
28    return "x86-32";
29#elif ARCH_CPU_64_BITS
30  return "x86-64";
31#else
32  return "x86-32";
33#endif  // defined(OS_WIN)
34
35#else
36#error Architecture not supported.
37#endif
38}
39
40std::string GetCpuFeatures() {
41  // PNaCl's translator from pexe to nexe can be told exactly what
42  // capabilities the user's machine has because the pexe to nexe
43  // translation is specific to the machine, and CPU information goes
44  // into the translation cache. This allows the translator to generate
45  // faster code.
46  //
47  // Care must be taken to avoid instructions which aren't supported by
48  // the NaCl sandbox. Ideally the translator would do this, but there's
49  // no point in not doing the whitelist here.
50  //
51  // TODO(jfb) Some features are missing, either because the NaCl
52  //           sandbox doesn't support them, because base::CPU doesn't
53  //           detect them, or because they don't help vector shuffles
54  //           (and we omit them because it simplifies testing). Add the
55  //           other features.
56  //
57  // TODO(jfb) The following is x86-specific. The base::CPU class
58  //           doesn't handle other architectures very well, and we
59  //           should at least detect the presence of ARM's integer
60  //           divide.
61  std::vector<std::string> features;
62  base::CPU cpu;
63
64  // On x86, SSE features are ordered: the most recent one implies the
65  // others. Care is taken here to only specify the latest SSE version,
66  // whereas non-SSE features don't follow this model: POPCNT is
67  // effectively always implied by SSE4.2 but has to be specified
68  // separately.
69  //
70  // TODO: AVX2, AVX, SSE 4.2.
71  if (cpu.has_sse41()) features.push_back("+sse4.1");
72  // TODO: SSE 4A, SSE 4.
73  else if (cpu.has_ssse3()) features.push_back("+ssse3");
74  // TODO: SSE 3
75  else if (cpu.has_sse2()) features.push_back("+sse2");
76
77  // TODO: AES, POPCNT, LZCNT, ...
78  return JoinString(features, ',');
79}
80
81}  // namespace nacl
82