2018-01-30 12:56:18 +00:00
|
|
|
/* This file is part of the dynarmic project.
|
|
|
|
* Copyright (c) 2018 MerryMage
|
|
|
|
* This software may be used and distributed according to the terms of the GNU
|
|
|
|
* General Public License version 2 or any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "backend_x64/abi.h"
|
|
|
|
#include "backend_x64/block_of_code.h"
|
|
|
|
#include "backend_x64/emit_x64.h"
|
|
|
|
#include "common/aes.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "frontend/ir/microinstruction.h"
|
|
|
|
#include "frontend/ir/opcodes.h"
|
|
|
|
|
|
|
|
namespace Dynarmic::BackendX64 {
|
|
|
|
|
|
|
|
using namespace Xbyak::util;
|
2018-02-03 22:48:50 +00:00
|
|
|
|
|
|
|
using AESFn = void(Common::AES::State&, const Common::AES::State&);
|
2018-01-30 12:56:18 +00:00
|
|
|
|
2018-02-03 18:16:02 +00:00
|
|
|
static void EmitAESFunction(std::array<Argument, 3> args, EmitContext& ctx, BlockOfCode& code,
|
|
|
|
IR::Inst* inst, AESFn fn) {
|
2018-02-03 22:48:50 +00:00
|
|
|
constexpr u32 stack_space = static_cast<u32>(sizeof(Common::AES::State)) * 2;
|
2018-01-30 12:56:18 +00:00
|
|
|
const Xbyak::Xmm input = ctx.reg_alloc.UseXmm(args[0]);
|
|
|
|
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]);
|
2018-02-03 22:48:50 +00:00
|
|
|
code.lea(code.ABI_PARAM2, ptr[rsp + ABI_SHADOW_SPACE + sizeof(Common::AES::State)]);
|
2018-01-30 12:56:18 +00:00
|
|
|
|
|
|
|
code.movaps(xword[code.ABI_PARAM2], input);
|
|
|
|
|
|
|
|
code.CallFunction(fn);
|
|
|
|
|
|
|
|
code.movaps(xmm0, xword[rsp + ABI_SHADOW_SPACE]);
|
|
|
|
|
|
|
|
// Free memory
|
|
|
|
code.add(rsp, stack_space + ABI_SHADOW_SPACE);
|
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, xmm0);
|
|
|
|
}
|
|
|
|
|
2018-02-03 22:20:21 +00:00
|
|
|
void EmitX64::EmitAESDecryptSingleRound(EmitContext& ctx, IR::Inst* inst) {
|
|
|
|
auto args = ctx.reg_alloc.GetArgumentInfo(inst);
|
|
|
|
|
2018-02-03 22:48:50 +00:00
|
|
|
EmitAESFunction(args, ctx, code, inst, Common::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);
|
|
|
|
|
2018-02-03 22:48:50 +00:00
|
|
|
EmitAESFunction(args, ctx, code, inst, Common::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);
|
|
|
|
|
2018-02-03 14:28:57 +00:00
|
|
|
if (code.DoesCpuSupport(Xbyak::util::Cpu::tAESNI)) {
|
2018-01-30 12:56:18 +00:00
|
|
|
const Xbyak::Xmm operand = ctx.reg_alloc.UseXmm(args[0]);
|
|
|
|
const Xbyak::Xmm result = ctx.reg_alloc.ScratchXmm();
|
|
|
|
|
2018-02-03 14:28:57 +00:00
|
|
|
code.aesimc(result, operand);
|
2018-01-30 12:56:18 +00:00
|
|
|
|
|
|
|
ctx.reg_alloc.DefineValue(inst, result);
|
|
|
|
} else {
|
2018-02-03 22:48:50 +00:00
|
|
|
EmitAESFunction(args, ctx, code, inst, Common::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);
|
2018-02-03 22:48:50 +00:00
|
|
|
EmitAESFunction(args, ctx, code, inst, Common::AES::MixColumns);
|
2018-01-30 12:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Dynarmic::BackendX64
|