cpu_s390x.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2020 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package cpu
  5. const cacheLineSize = 256
  6. func initOptions() {
  7. options = []option{
  8. {Name: "zarch", Feature: &S390X.HasZARCH, Required: true},
  9. {Name: "stfle", Feature: &S390X.HasSTFLE, Required: true},
  10. {Name: "ldisp", Feature: &S390X.HasLDISP, Required: true},
  11. {Name: "eimm", Feature: &S390X.HasEIMM, Required: true},
  12. {Name: "dfp", Feature: &S390X.HasDFP},
  13. {Name: "etf3eh", Feature: &S390X.HasETF3EH},
  14. {Name: "msa", Feature: &S390X.HasMSA},
  15. {Name: "aes", Feature: &S390X.HasAES},
  16. {Name: "aescbc", Feature: &S390X.HasAESCBC},
  17. {Name: "aesctr", Feature: &S390X.HasAESCTR},
  18. {Name: "aesgcm", Feature: &S390X.HasAESGCM},
  19. {Name: "ghash", Feature: &S390X.HasGHASH},
  20. {Name: "sha1", Feature: &S390X.HasSHA1},
  21. {Name: "sha256", Feature: &S390X.HasSHA256},
  22. {Name: "sha3", Feature: &S390X.HasSHA3},
  23. {Name: "sha512", Feature: &S390X.HasSHA512},
  24. {Name: "vx", Feature: &S390X.HasVX},
  25. {Name: "vxe", Feature: &S390X.HasVXE},
  26. }
  27. }
  28. // bitIsSet reports whether the bit at index is set. The bit index
  29. // is in big endian order, so bit index 0 is the leftmost bit.
  30. func bitIsSet(bits []uint64, index uint) bool {
  31. return bits[index/64]&((1<<63)>>(index%64)) != 0
  32. }
  33. // facility is a bit index for the named facility.
  34. type facility uint8
  35. const (
  36. // mandatory facilities
  37. zarch facility = 1 // z architecture mode is active
  38. stflef facility = 7 // store-facility-list-extended
  39. ldisp facility = 18 // long-displacement
  40. eimm facility = 21 // extended-immediate
  41. // miscellaneous facilities
  42. dfp facility = 42 // decimal-floating-point
  43. etf3eh facility = 30 // extended-translation 3 enhancement
  44. // cryptography facilities
  45. msa facility = 17 // message-security-assist
  46. msa3 facility = 76 // message-security-assist extension 3
  47. msa4 facility = 77 // message-security-assist extension 4
  48. msa5 facility = 57 // message-security-assist extension 5
  49. msa8 facility = 146 // message-security-assist extension 8
  50. msa9 facility = 155 // message-security-assist extension 9
  51. // vector facilities
  52. vx facility = 129 // vector facility
  53. vxe facility = 135 // vector-enhancements 1
  54. vxe2 facility = 148 // vector-enhancements 2
  55. )
  56. // facilityList contains the result of an STFLE call.
  57. // Bits are numbered in big endian order so the
  58. // leftmost bit (the MSB) is at index 0.
  59. type facilityList struct {
  60. bits [4]uint64
  61. }
  62. // Has reports whether the given facilities are present.
  63. func (s *facilityList) Has(fs ...facility) bool {
  64. if len(fs) == 0 {
  65. panic("no facility bits provided")
  66. }
  67. for _, f := range fs {
  68. if !bitIsSet(s.bits[:], uint(f)) {
  69. return false
  70. }
  71. }
  72. return true
  73. }
  74. // function is the code for the named cryptographic function.
  75. type function uint8
  76. const (
  77. // KM{,A,C,CTR} function codes
  78. aes128 function = 18 // AES-128
  79. aes192 function = 19 // AES-192
  80. aes256 function = 20 // AES-256
  81. // K{I,L}MD function codes
  82. sha1 function = 1 // SHA-1
  83. sha256 function = 2 // SHA-256
  84. sha512 function = 3 // SHA-512
  85. sha3_224 function = 32 // SHA3-224
  86. sha3_256 function = 33 // SHA3-256
  87. sha3_384 function = 34 // SHA3-384
  88. sha3_512 function = 35 // SHA3-512
  89. shake128 function = 36 // SHAKE-128
  90. shake256 function = 37 // SHAKE-256
  91. // KLMD function codes
  92. ghash function = 65 // GHASH
  93. )
  94. // queryResult contains the result of a Query function
  95. // call. Bits are numbered in big endian order so the
  96. // leftmost bit (the MSB) is at index 0.
  97. type queryResult struct {
  98. bits [2]uint64
  99. }
  100. // Has reports whether the given functions are present.
  101. func (q *queryResult) Has(fns ...function) bool {
  102. if len(fns) == 0 {
  103. panic("no function codes provided")
  104. }
  105. for _, f := range fns {
  106. if !bitIsSet(q.bits[:], uint(f)) {
  107. return false
  108. }
  109. }
  110. return true
  111. }
  112. func doinit() {
  113. initS390Xbase()
  114. // We need implementations of stfle, km and so on
  115. // to detect cryptographic features.
  116. if !haveAsmFunctions() {
  117. return
  118. }
  119. // optional cryptographic functions
  120. if S390X.HasMSA {
  121. aes := []function{aes128, aes192, aes256}
  122. // cipher message
  123. km, kmc := kmQuery(), kmcQuery()
  124. S390X.HasAES = km.Has(aes...)
  125. S390X.HasAESCBC = kmc.Has(aes...)
  126. if S390X.HasSTFLE {
  127. facilities := stfle()
  128. if facilities.Has(msa4) {
  129. kmctr := kmctrQuery()
  130. S390X.HasAESCTR = kmctr.Has(aes...)
  131. }
  132. if facilities.Has(msa8) {
  133. kma := kmaQuery()
  134. S390X.HasAESGCM = kma.Has(aes...)
  135. }
  136. }
  137. // compute message digest
  138. kimd := kimdQuery() // intermediate (no padding)
  139. klmd := klmdQuery() // last (padding)
  140. S390X.HasSHA1 = kimd.Has(sha1) && klmd.Has(sha1)
  141. S390X.HasSHA256 = kimd.Has(sha256) && klmd.Has(sha256)
  142. S390X.HasSHA512 = kimd.Has(sha512) && klmd.Has(sha512)
  143. S390X.HasGHASH = kimd.Has(ghash) // KLMD-GHASH does not exist
  144. sha3 := []function{
  145. sha3_224, sha3_256, sha3_384, sha3_512,
  146. shake128, shake256,
  147. }
  148. S390X.HasSHA3 = kimd.Has(sha3...) && klmd.Has(sha3...)
  149. }
  150. }