Skip to content

Commit

Permalink
S390x support (#1103)
Browse files Browse the repository at this point in the history
* s390x support

* - Fix for FrodoKEM-SHAKE for big endian support
- Fix unused variable in Keccak code on big endian
  • Loading branch information
bhess authored Oct 12, 2021
1 parent 59c1987 commit 0164040
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 4 deletions.
15 changes: 13 additions & 2 deletions .CMake/compiler_opts.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,24 @@ if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU")
endif()

if(OQS_OPT_TARGET STREQUAL "generic")
# Assume sensible default like -march=x86-64, -march=armv8-a, etc.
set(OQS_OPT_FLAG "")
if(ARCH_S390X)
# At least z9-109 is needed for 'stckf' in benchmarking code.
# gcc's default is z900 (older than z9-109), clang's default and minimum is z10.
# setting to z10 as sensible default.
set(OQS_OPT_FLAG "-march=z10")
else()
# Assume sensible default like -march=x86-64, -march=armv8-a, etc.
set(OQS_OPT_FLAG "")
endif()
elseif(OQS_OPT_TARGET STREQUAL "auto")
if(ARCH_X86_64)
set(OQS_OPT_FLAG "-march=native")
elseif(ARCH_ARM64v8 AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(OQS_OPT_FLAG "-mcpu=native")
elseif(ARCH_ARM64v8 AND CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(OQS_OPT_FLAG "-mcpu=native")
elseif(ARCH_S390X)
set(OQS_OPT_FLAG "-march=native")
else()
message(WARNING "Setting OQS_OPT_TARGET=AUTO may not produce optimized code on this system.")
endif()
Expand All @@ -39,6 +48,8 @@ if(CMAKE_C_COMPILER_ID MATCHES "Clang|GNU")
set(OQS_OPT_FLAG "-march=${OQS_OPT_TARGET}")
elseif(ARCH_ARM64v8 OR ARCH_ARM32v7)
set(OQS_OPT_FLAG "-mcpu=${OQS_OPT_TARGET}")
elseif(ARCH_S390X)
set(OQS_OPT_FLAG "-march=${OQS_OPT_TARGET}")
endif()
endif()

Expand Down
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ jobs:
script:
- mkdir build && cd build && cmake -GNinja .. && cmake -LA .. && ninja
- cd build & ninja run_tests
- arch: s390x
os: linux
dist: focal
compiler: gcc
if: NOT branch =~ /^ghactionsonly-/
script:
- mkdir build && cd build && cmake -DOQS_ENABLE_KEM_BIKE=OFF -GNinja .. && cmake -LA .. && ninja
- cd build & ninja run_tests
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc64le")
if(${OQS_DIST_BUILD})
set(OQS_DIST_PPC64LE_BUILD ON)
endif()
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "s390x")
set(ARCH "s390x")
set(ARCH_S390X ON)
if(${OQS_DIST_BUILD})
set(OQS_DIST_S390X_BUILD ON)
endif()
elseif(OQS_PERMIT_UNSUPPORTED_ARCHITECTURE)
message(WARNING "Unknown or unsupported processor: " ${CMAKE_SYSTEM_PROCESSOR})
message(WARNING "Compilation on an unsupported processor should only be used for testing, as it may result an insecure configuration, for example due to variable-time instructions leaking secret information.")
Expand Down
5 changes: 5 additions & 0 deletions src/common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ static void set_available_cpu_extensions(void) {
/* mark that this function has been called */
cpu_ext_data[OQS_CPU_EXT_INIT] = 1;
}
#elif defined(OQS_DIST_S390X_BUILD)
static void set_available_cpu_extensions(void) {
/* mark that this function has been called */
cpu_ext_data[OQS_CPU_EXT_INIT] = 1;
}
#elif defined(OQS_DIST_BUILD)
static void set_available_cpu_extensions(void) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,11 @@ void KeccakP1600_ExtractAndAddBytesInLane(const void *state, unsigned int lanePo

void KeccakP1600_ExtractAndAddLanes(const void *state, const unsigned char *input, unsigned char *output, unsigned int laneCount) {
unsigned int i;
uint64_t lane;
#if (PLATFORM_BYTE_ORDER != IS_LITTLE_ENDIAN)
unsigned char temp[8];
unsigned int j;
#else
uint64_t lane;
#endif

for (i = 0; i < laneCount; i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/kem/frodokem/external/frodo_macrify_shake_portable.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int frodo_mul_add_sa_plus_e_shake_portable(uint16_t *out, const uint16_t *s, con
for (j = 0; j < 4; j++) {
uint16_t sp = s[i*PARAMS_N + kk + j];
for (int k = 0; k < PARAMS_N; k++) { // Matrix-vector multiplication
sum[k] += (uint16_t) ((uint32_t) sp * (uint32_t) a_cols[(t+j)*PARAMS_N + k]);
sum[k] += (uint16_t) ((uint32_t) sp * (uint32_t) UINT16_TO_LE(a_cols[(t+j)*PARAMS_N + k]));
}
}
for (int k = 0; k < PARAMS_N; k++){
Expand Down
2 changes: 2 additions & 0 deletions src/kem/sike/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ if(OQS_ENABLE_KEM_sike_p434 OR
endif()
elseif(ARCH_PPC64LE)
target_compile_definitions(sike PRIVATE _GENERIC_ _PPC64LE_)
elseif(ARCH_S390X)
target_compile_definitions(sike PRIVATE _GENERIC_ _S390X_)
endif()

if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
Expand Down
5 changes: 5 additions & 0 deletions tests/ds_benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ static uint64_t _bench_rdtsc(void) {
__asm__ volatile("mrc p15, 0, %0, c9, c13, 0\t\n"
: "=r"(value));
return value;
#elif defined(__s390x__)
#define USING_TIME_RATHER_THAN_CYCLES
uint64_t tod;
__asm__ volatile("stckf %0\n" : "=Q" (tod) : : "cc");
return (tod * 1000 / 4096);
#else
#define USING_TIME_RATHER_THAN_CYCLES
struct timespec time;
Expand Down

0 comments on commit 0164040

Please sign in to comment.