Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 : #ifndef KERNEL_PC_FIXER_H
11 : #define KERNEL_PC_FIXER_H
12 :
13 : #include <cstdint>
14 : #include <memory>
15 : #include <string>
16 : #include <vector>
17 :
18 : namespace Adx {
19 :
20 : struct PcFixEntry {
21 : uint32_t errInfoRegNum;
22 : uint64_t srcMask;
23 : uint64_t dstMask;
24 : };
25 :
26 : struct PcFixGroup {
27 : uint32_t moduleId;
28 : uint32_t aicErrMask;
29 : std::vector<PcFixEntry> entries;
30 : };
31 :
32 : class PcFixerInterface {
33 : public:
34 35 : PcFixerInterface() = default;
35 35 : virtual ~PcFixerInterface() = default;
36 :
37 : // 单个寄存器项的最大长度:"NAME=0xVALUE " = 名字(<=REG_NAME_MAX_LEN) + "=0x" + %x(<=8) + 空格。
38 : static constexpr size_t REG_NAME_MAX_LEN = 18U;
39 : static constexpr size_t REG_ITEM_MAX_LEN = REG_NAME_MAX_LEN + 3U + 8U + 1U;
40 :
41 : uint64_t FixPc(uint64_t pc, const uint32_t errReg[], size_t errRegLen);
42 : // 逐项返回 "NAME=0xVALUE" 形式的寄存器 dump,调用方可按固定项数分行打印,避免超出单条日志上限。
43 : virtual std::vector<std::string> GetErrorRegisterItems(const uint32_t errReg[], size_t errRegLen) const = 0;
44 : // 完整寄存器 dump 串(各项以空格分隔),等价于 GetErrorRegisterItems 的拼接结果。
45 : std::string GetErrorRegisters(const uint32_t errReg[], size_t errRegLen) const;
46 :
47 : protected:
48 : uint32_t errStartIdx_{0};
49 : uint32_t errCount_{0};
50 : std::vector<std::vector<PcFixGroup>> table_;
51 :
52 : static void ReplacePcBits(uint64_t& pc, uint32_t regValue, uint64_t srcMask, uint64_t dstMask);
53 : std::vector<const PcFixGroup*> GetMatchedGroups(const uint32_t errReg[], size_t errRegLen) const;
54 : virtual std::string GetModuleName(uint32_t moduleId) const = 0;
55 : };
56 :
57 : class CloudV2PcFixer : public PcFixerInterface {
58 : public:
59 : CloudV2PcFixer();
60 : std::vector<std::string> GetErrorRegisterItems(const uint32_t errReg[], size_t errRegLen) const override;
61 :
62 : private:
63 : std::string GetModuleName(uint32_t moduleId) const override;
64 : };
65 :
66 : class CloudV4PcFixer : public PcFixerInterface {
67 : public:
68 : CloudV4PcFixer();
69 : std::vector<std::string> GetErrorRegisterItems(const uint32_t errReg[], size_t errRegLen) const override;
70 :
71 : private:
72 : std::string GetModuleName(uint32_t moduleId) const override;
73 : };
74 :
75 : class CloudV5PcFixer : public PcFixerInterface {
76 : public:
77 : CloudV5PcFixer();
78 : std::vector<std::string> GetErrorRegisterItems(const uint32_t errReg[], size_t errRegLen) const override;
79 :
80 : private:
81 : std::string GetModuleName(uint32_t moduleId) const override;
82 : };
83 :
84 : class PcFixerFactory {
85 : public:
86 : static PcFixerInterface* GetInstance();
87 :
88 : private:
89 : static std::unique_ptr<PcFixerInterface> instance_;
90 : };
91 :
92 : } // namespace Adx
93 :
94 : #endif // KERNEL_PC_FIXER_H
|