summaryrefslogtreecommitdiffstats
path: root/SourceLevelDebugPkg/Library/DebugAgent/DebugAgentCommon/Ia32/ArchReadGroupRegister.c
blob: 59ebd002558215dcd373102725b0fe7a951692c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/** @file
  IA32 Group registers read support functions.

  Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
  This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php.

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include "DebugAgent.h"

/**
  Read group register of Segment Base.

  @param[in] CpuContext           Pointer to saved CPU context.
  @param[in] RegisterGroupSegBase Pointer to Group registers.

**/
VOID
ReadRegisterGroupSegBase (
  IN DEBUG_CPU_CONTEXT                              *CpuContext,
  IN DEBUG_DATA_REPONSE_READ_REGISTER_GROUP_SEGBASE *RegisterGroupSegBase
  )
{
  IA32_DESCRIPTOR               *Ia32Descriptor;
  IA32_GDT                      *Ia32Gdt;
  UINTN                         Index;

  Ia32Descriptor = (IA32_DESCRIPTOR *) CpuContext->Gdtr;
  Ia32Gdt = (IA32_GDT *) (Ia32Descriptor->Base);

  Index = CpuContext->Cs / 8;
  RegisterGroupSegBase->CsBas = (Ia32Gdt[Index].Bits.BaseLow) + (Ia32Gdt[Index].Bits.BaseMid << 16) + (Ia32Gdt[Index].Bits.BaseMid << 24);
  Index = CpuContext->Ss / 8;
  RegisterGroupSegBase->SsBas = (Ia32Gdt[Index].Bits.BaseLow) + (Ia32Gdt[Index].Bits.BaseMid << 16) + (Ia32Gdt[Index].Bits.BaseMid << 24);
  Index = CpuContext->Gs / 8;
  RegisterGroupSegBase->GsBas = (Ia32Gdt[Index].Bits.BaseLow) + (Ia32Gdt[Index].Bits.BaseMid << 16) + (Ia32Gdt[Index].Bits.BaseMid << 24);
  Index = CpuContext->Fs / 8;
  RegisterGroupSegBase->FsBas = (Ia32Gdt[Index].Bits.BaseLow) + (Ia32Gdt[Index].Bits.BaseMid << 16) + (Ia32Gdt[Index].Bits.BaseMid << 24);
  Index = CpuContext->Es / 8;
  RegisterGroupSegBase->EsBas = (Ia32Gdt[Index].Bits.BaseLow) + (Ia32Gdt[Index].Bits.BaseMid << 16) + (Ia32Gdt[Index].Bits.BaseMid << 24);
  Index = CpuContext->Ds / 8;
  RegisterGroupSegBase->DsBas = (Ia32Gdt[Index].Bits.BaseLow) + (Ia32Gdt[Index].Bits.BaseMid << 16) + (Ia32Gdt[Index].Bits.BaseMid << 24);

  RegisterGroupSegBase->LdtBas = 0;
  RegisterGroupSegBase->TssBas = 0;
}

/**
  Read gourp register of Segment Limit.

  @param[in] CpuContext           Pointer to saved CPU context.
  @param[in] RegisterGroupSegLim  Pointer to Group registers.

**/
VOID
ReadRegisterGroupSegLim (
  IN DEBUG_CPU_CONTEXT                             *CpuContext,
  IN DEBUG_DATA_REPONSE_READ_REGISTER_GROUP_SEGLIM *RegisterGroupSegLim
  )
{
  IA32_DESCRIPTOR               *Ia32Descriptor;
  IA32_GDT                      *Ia32Gdt;
  UINTN                         Index;

  Ia32Descriptor = (IA32_DESCRIPTOR *) CpuContext->Gdtr;
  Ia32Gdt = (IA32_GDT *) (Ia32Descriptor->Base);

  Index = CpuContext->Cs / 8;
  RegisterGroupSegLim->CsLim = Ia32Gdt[Index].Bits.LimitLow + (Ia32Gdt[Index].Bits.LimitHigh << 16);
  if (Ia32Gdt[Index].Bits.Granularity == 1) {
    RegisterGroupSegLim->CsLim = (RegisterGroupSegLim->CsLim << 12) | 0xfff;
  }

  Index = CpuContext->Ss / 8;
  RegisterGroupSegLim->SsLim = Ia32Gdt[Index].Bits.LimitLow + (Ia32Gdt[Index].Bits.LimitHigh << 16);
  if (Ia32Gdt[Index].Bits.Granularity == 1) {
    RegisterGroupSegLim->SsLim = (RegisterGroupSegLim->SsLim << 12) | 0xfff;
  }

  Index = CpuContext->Gs / 8;
  RegisterGroupSegLim->GsLim = Ia32Gdt[Index].Bits.LimitLow + (Ia32Gdt[Index].Bits.LimitHigh << 16);
  if (Ia32Gdt[Index].Bits.Granularity == 1) {
    RegisterGroupSegLim->GsLim = (RegisterGroupSegLim->GsLim << 12) | 0xfff;
  }

  Index = CpuContext->Fs / 8;
  RegisterGroupSegLim->FsLim = Ia32Gdt[Index].Bits.LimitLow + (Ia32Gdt[Index].Bits.LimitHigh << 16);
  if (Ia32Gdt[Index].Bits.Granularity == 1) {
    RegisterGroupSegLim->FsLim = (RegisterGroupSegLim->FsLim << 12) | 0xfff;
  }

  Index = CpuContext->Es / 8;
  RegisterGroupSegLim->EsLim = Ia32Gdt[Index].Bits.LimitLow + (Ia32Gdt[Index].Bits.LimitHigh << 16);
  if (Ia32Gdt[Index].Bits.Granularity == 1) {
    RegisterGroupSegLim->EsLim = (RegisterGroupSegLim->EsLim << 12) | 0xfff;
  }

  Index = CpuContext->Ds / 8;
  RegisterGroupSegLim->DsLim = Ia32Gdt[Index].Bits.LimitLow + (Ia32Gdt[Index].Bits.LimitHigh << 16);
  if (Ia32Gdt[Index].Bits.Granularity == 1) {
    RegisterGroupSegLim->DsLim = (RegisterGroupSegLim->DsLim << 12) | 0xfff;
  }

  RegisterGroupSegLim->LdtLim = 0xffff;
  RegisterGroupSegLim->TssLim = 0xffff;
}

/**
  Read group register by group index.

  @param[in] CpuContext           Pointer to saved CPU context.
  @param[in] GroupIndex           Group Index.

  @retval RETURN_SUCCESS         Read successfully.
  @retval RETURN_NOT_SUPPORTED   Group index cannot be supported.

**/
RETURN_STATUS
ArchReadRegisterGroup (
  IN DEBUG_CPU_CONTEXT                             *CpuContext,
  IN UINT8                                         GroupIndex
  )
{
  DEBUG_DATA_REPONSE_READ_REGISTER_GROUP  RegisterGroup;
  DEBUG_DATA_REPONSE_READ_REGISTER_GROUP_SEGLIM   RegisterGroupSegLim;
  DEBUG_DATA_REPONSE_READ_REGISTER_GROUP_SEGBASE  RegisterGroupSegBase;

  switch (GroupIndex) {
  case SOFT_DEBUGGER_REGISTER_GROUP_GPDRS32:
    ReadRegisterGroup (CpuContext, &RegisterGroup);
    SendDataResponsePacket (CpuContext, (UINT8 *) &RegisterGroup, (UINT16) sizeof (DEBUG_DATA_REPONSE_READ_REGISTER_GROUP));
    break;

  case SOFT_DEBUGGER_REGISTER_GROUP_SEGMENT_LIMITS32:
    ReadRegisterGroupSegLim (CpuContext, &RegisterGroupSegLim);
    SendDataResponsePacket (CpuContext, (UINT8 *) &RegisterGroupSegLim, (UINT16) sizeof (DEBUG_DATA_REPONSE_READ_REGISTER_GROUP_SEGLIM));
    break;

  case SOFT_DEBUGGER_REGISTER_GROUP_SEGMENT_BASES32:
    ReadRegisterGroupSegBase (CpuContext, &RegisterGroupSegBase);
    SendDataResponsePacket (CpuContext, (UINT8 *) &RegisterGroupSegBase, (UINT16) sizeof (DEBUG_DATA_REPONSE_READ_REGISTER_GROUP_SEGBASE));
    break;

  default:
    return RETURN_UNSUPPORTED;
  }

  return RETURN_SUCCESS;
}

/**
  Read segment selector by register index.

  @param[in] CpuContext           Pointer to saved CPU context.
  @param[in] RegisterIndex        Register Index.

  @return Value of segment selector.

**/
UINT64
ReadRegisterSelectorByIndex (
  IN DEBUG_CPU_CONTEXT                       *CpuContext,
  IN UINT8                                   RegisterIndex
  )
{
  IA32_DESCRIPTOR               *Ia32Descriptor;
  IA32_GDT                      *Ia32Gdt;
  UINT16                        Selector;
  UINT32                        Data32;

  Ia32Descriptor = (IA32_DESCRIPTOR *) CpuContext->Gdtr;
  Ia32Gdt = (IA32_GDT *) (Ia32Descriptor->Base);

  Selector = 0;

  switch (RegisterIndex) {
  case SOFT_DEBUGGER_REGISTER_CSAS:
    Selector = (UINT16) CpuContext->Cs;
    break;
  case SOFT_DEBUGGER_REGISTER_SSAS:
    Selector = (UINT16) CpuContext->Ss;
    break;
  case SOFT_DEBUGGER_REGISTER_GSAS:
    Selector = (UINT16) CpuContext->Gs;
    break;
  case SOFT_DEBUGGER_REGISTER_FSAS:
    Selector = (UINT16) CpuContext->Fs;
    break;
  case SOFT_DEBUGGER_REGISTER_ESAS:
    Selector = (UINT16) CpuContext->Es;
    break;
  case SOFT_DEBUGGER_REGISTER_DSAS:
    Selector = (UINT16) CpuContext->Ds;
  case SOFT_DEBUGGER_REGISTER_LDTAS:
  case SOFT_DEBUGGER_REGISTER_TSSAS:
    return 0x00820000;
    break;
  }

  Data32 = (UINT32) RShiftU64 (Ia32Gdt[Selector / 8].Uint64, 24);
  return (Data32 & (UINT32)(~0xff)) | Selector;

}