dynarmic/src/backend/x64/emit_x64_aes.cpp

76 lines
2.3 KiB
C++
Raw Normal View History

2018-01-30 12:56:18 +00:00
/* This file is part of the dynarmic project.
* Copyright (c) 2018 MerryMage
2020-04-23 15:25:11 +01:00
* SPDX-License-Identifier: 0BSD
2018-01-30 12:56:18 +00:00
*/
#include "backend/x64/abi.h"
#include "backend/x64/block_of_code.h"
#include "backend/x64/emit_x64.h"
2018-01-30 12:56:18 +00:00
#include "common/common_types.h"
#include "common/crypto/aes.h"
2018-01-30 12:56:18 +00:00
#include "frontend/ir/microinstruction.h"
namespace Dynarmic::Backend::X64 {
2018-01-30 12:56:18 +00:00
using namespace Xbyak::util;
namespace AES = Common::Crypto::AES;
using AESFn = void(AES::State&, const AES::State&);
2018-01-30 12:56:18 +00:00
static void EmitAESFunction(RegAlloc::ArgumentInfo args, EmitContext& ctx, BlockOfCode& code,
2018-02-03 18:16:02 +00:00
IR::Inst* inst, AESFn fn) {
constexpr u32 stack_space = static_cast<u32>(sizeof(AES::State)) * 2;
2018-01-30 12:56:18 +00:00
const Xbyak::Xmm input = ctx.reg_alloc.UseXmm(args[0]);
2018-07-31 20:53:33 +01:00
const Xbyak::Xmm result = ctx.reg_alloc.ScratchXmm();
2018-01-30 12:56:18 +00:00
ctx.reg_alloc.EndOfAllocScope();
ctx.reg_alloc.HostCall(nullptr);
code.sub(rsp, stack_space + ABI_SHADOW_SPACE);
code.lea(code.ABI_PARAM1, ptr[rsp + ABI_SHADOW_SPACE]);
code.lea(code.ABI_PARAM2, ptr[rsp + ABI_SHADOW_SPACE + sizeof(AES::State)]);
2018-01-30 12:56:18 +00:00
code.movaps(xword[code.ABI_PARAM2], input);
code.CallFunction(fn);
2018-07-31 20:53:33 +01:00
code.movaps(result, xword[rsp + ABI_SHADOW_SPACE]);
2018-01-30 12:56:18 +00:00
// Free memory
code.add(rsp, stack_space + ABI_SHADOW_SPACE);
2018-07-31 20:53:33 +01:00
ctx.reg_alloc.DefineValue(inst, result);
2018-01-30 12:56:18 +00:00
}
2018-02-03 22:20:21 +00:00
void EmitX64::EmitAESDecryptSingleRound(EmitContext& ctx, IR::Inst* inst) {
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
EmitAESFunction(args, ctx, code, inst, AES::DecryptSingleRound);
2018-02-03 22:20:21 +00:00
}
2018-02-03 18:16:02 +00:00
void EmitX64::EmitAESEncryptSingleRound(EmitContext& ctx, IR::Inst* inst) {
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
EmitAESFunction(args, ctx, code, inst, AES::EncryptSingleRound);
2018-02-03 18:16:02 +00:00
}
2018-01-30 12:56:18 +00:00
void EmitX64::EmitAESInverseMixColumns(EmitContext& ctx, IR::Inst* inst) {
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
if (code.HasAESNI()) {
const Xbyak::Xmm data = ctx.reg_alloc.UseScratchXmm(args[0]);
2018-01-30 12:56:18 +00:00
code.aesimc(data, data);
2018-01-30 12:56:18 +00:00
ctx.reg_alloc.DefineValue(inst, data);
2018-01-30 12:56:18 +00:00
} else {
EmitAESFunction(args, ctx, code, inst, AES::InverseMixColumns);
2018-01-30 12:56:18 +00:00
}
}
void EmitX64::EmitAESMixColumns(EmitContext& ctx, IR::Inst* inst) {
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
EmitAESFunction(args, ctx, code, inst, AES::MixColumns);
2018-01-30 12:56:18 +00:00
}
} // namespace Dynarmic::Backend::X64