interrupts.cc revision 6061:385c8482bf14
1/*
2 * Copyright (c) 2008 The Hewlett-Packard Development Company
3 * All rights reserved.
4 *
5 * Redistribution and use of this software in source and binary forms,
6 * with or without modification, are permitted provided that the
7 * following conditions are met:
8 *
9 * The software must be used only for Non-Commercial Use which means any
10 * use which is NOT directed to receiving any direct monetary
11 * compensation for, or commercial advantage from such use.  Illustrative
12 * examples of non-commercial use are academic research, personal study,
13 * teaching, education and corporate research & development.
14 * Illustrative examples of commercial use are distributing products for
15 * commercial advantage and providing services using the software for
16 * commercial advantage.
17 *
18 * If you wish to use this software or functionality therein that may be
19 * covered by patents for commercial use, please contact:
20 *     Director of Intellectual Property Licensing
21 *     Office of Strategy and Technology
22 *     Hewlett-Packard Company
23 *     1501 Page Mill Road
24 *     Palo Alto, California  94304
25 *
26 * Redistributions of source code must retain the above copyright notice,
27 * this list of conditions and the following disclaimer.  Redistributions
28 * in binary form must reproduce the above copyright notice, this list of
29 * conditions and the following disclaimer in the documentation and/or
30 * other materials provided with the distribution.  Neither the name of
31 * the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
32 * contributors may be used to endorse or promote products derived from
33 * this software without specific prior written permission.  No right of
34 * sublicense is granted herewith.  Derivatives of the software and
35 * output created using the software may be prepared, but only for
36 * Non-Commercial Uses.  Derivatives of the software may be shared with
37 * others provided: (i) the others agree to abide by the list of
38 * conditions herein which includes the Non-Commercial Use restrictions;
39 * and (ii) such Derivatives of the software include the above copyright
40 * notice to acknowledge the contribution from this software where
41 * applicable, this list of conditions and the disclaimer below.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 *
55 * Authors: Gabe Black
56 */
57
58#include "arch/x86/apicregs.hh"
59#include "arch/x86/interrupts.hh"
60#include "arch/x86/intmessage.hh"
61#include "cpu/base.hh"
62#include "mem/packet_access.hh"
63#include "sim/system.hh"
64
65int
66divideFromConf(uint32_t conf)
67{
68    // This figures out what division we want from the division configuration
69    // register in the local APIC. The encoding is a little odd but it can
70    // be deciphered fairly easily.
71    int shift = ((conf & 0x8) >> 1) | (conf & 0x3);
72    shift = (shift + 1) % 8;
73    return 1 << shift;
74}
75
76namespace X86ISA
77{
78
79ApicRegIndex
80decodeAddr(Addr paddr)
81{
82    ApicRegIndex regNum;
83    paddr &= ~mask(3);
84    switch (paddr)
85    {
86      case 0x20:
87        regNum = APIC_ID;
88        break;
89      case 0x30:
90        regNum = APIC_VERSION;
91        break;
92      case 0x80:
93        regNum = APIC_TASK_PRIORITY;
94        break;
95      case 0x90:
96        regNum = APIC_ARBITRATION_PRIORITY;
97        break;
98      case 0xA0:
99        regNum = APIC_PROCESSOR_PRIORITY;
100        break;
101      case 0xB0:
102        regNum = APIC_EOI;
103        break;
104      case 0xD0:
105        regNum = APIC_LOGICAL_DESTINATION;
106        break;
107      case 0xE0:
108        regNum = APIC_DESTINATION_FORMAT;
109        break;
110      case 0xF0:
111        regNum = APIC_SPURIOUS_INTERRUPT_VECTOR;
112        break;
113      case 0x100:
114      case 0x108:
115      case 0x110:
116      case 0x118:
117      case 0x120:
118      case 0x128:
119      case 0x130:
120      case 0x138:
121      case 0x140:
122      case 0x148:
123      case 0x150:
124      case 0x158:
125      case 0x160:
126      case 0x168:
127      case 0x170:
128      case 0x178:
129        regNum = APIC_IN_SERVICE((paddr - 0x100) / 0x8);
130        break;
131      case 0x180:
132      case 0x188:
133      case 0x190:
134      case 0x198:
135      case 0x1A0:
136      case 0x1A8:
137      case 0x1B0:
138      case 0x1B8:
139      case 0x1C0:
140      case 0x1C8:
141      case 0x1D0:
142      case 0x1D8:
143      case 0x1E0:
144      case 0x1E8:
145      case 0x1F0:
146      case 0x1F8:
147        regNum = APIC_TRIGGER_MODE((paddr - 0x180) / 0x8);
148        break;
149      case 0x200:
150      case 0x208:
151      case 0x210:
152      case 0x218:
153      case 0x220:
154      case 0x228:
155      case 0x230:
156      case 0x238:
157      case 0x240:
158      case 0x248:
159      case 0x250:
160      case 0x258:
161      case 0x260:
162      case 0x268:
163      case 0x270:
164      case 0x278:
165        regNum = APIC_INTERRUPT_REQUEST((paddr - 0x200) / 0x8);
166        break;
167      case 0x280:
168        regNum = APIC_ERROR_STATUS;
169        break;
170      case 0x300:
171        regNum = APIC_INTERRUPT_COMMAND_LOW;
172        break;
173      case 0x310:
174        regNum = APIC_INTERRUPT_COMMAND_HIGH;
175        break;
176      case 0x320:
177        regNum = APIC_LVT_TIMER;
178        break;
179      case 0x330:
180        regNum = APIC_LVT_THERMAL_SENSOR;
181        break;
182      case 0x340:
183        regNum = APIC_LVT_PERFORMANCE_MONITORING_COUNTERS;
184        break;
185      case 0x350:
186        regNum = APIC_LVT_LINT0;
187        break;
188      case 0x360:
189        regNum = APIC_LVT_LINT1;
190        break;
191      case 0x370:
192        regNum = APIC_LVT_ERROR;
193        break;
194      case 0x380:
195        regNum = APIC_INITIAL_COUNT;
196        break;
197      case 0x390:
198        regNum = APIC_CURRENT_COUNT;
199        break;
200      case 0x3E0:
201        regNum = APIC_DIVIDE_CONFIGURATION;
202        break;
203      default:
204        // A reserved register field.
205        panic("Accessed reserved register field %#x.\n", paddr);
206        break;
207    }
208    return regNum;
209}
210}
211
212Tick
213X86ISA::Interrupts::read(PacketPtr pkt)
214{
215    Addr offset = pkt->getAddr() - pioAddr;
216    //Make sure we're at least only accessing one register.
217    if ((offset & ~mask(3)) != ((offset + pkt->getSize()) & ~mask(3)))
218        panic("Accessed more than one register at a time in the APIC!\n");
219    ApicRegIndex reg = decodeAddr(offset);
220    uint32_t val = htog(readReg(reg));
221    DPRINTF(LocalApic,
222            "Reading Local APIC register %d at offset %#x as %#x.\n",
223            reg, offset, val);
224    pkt->setData(((uint8_t *)&val) + (offset & mask(3)));
225    pkt->makeAtomicResponse();
226    return latency;
227}
228
229Tick
230X86ISA::Interrupts::write(PacketPtr pkt)
231{
232    Addr offset = pkt->getAddr() - pioAddr;
233    //Make sure we're at least only accessing one register.
234    if ((offset & ~mask(3)) != ((offset + pkt->getSize()) & ~mask(3)))
235        panic("Accessed more than one register at a time in the APIC!\n");
236    ApicRegIndex reg = decodeAddr(offset);
237    uint32_t val = regs[reg];
238    pkt->writeData(((uint8_t *)&val) + (offset & mask(3)));
239    DPRINTF(LocalApic,
240            "Writing Local APIC register %d at offset %#x as %#x.\n",
241            reg, offset, gtoh(val));
242    setReg(reg, gtoh(val));
243    pkt->makeAtomicResponse();
244    return latency;
245}
246void
247X86ISA::Interrupts::requestInterrupt(uint8_t vector,
248        uint8_t deliveryMode, bool level)
249{
250    /*
251     * Fixed and lowest-priority delivery mode interrupts are handled
252     * using the IRR/ISR registers, checking against the TPR, etc.
253     * The SMI, NMI, ExtInt, INIT, etc interrupts go straight through.
254     */
255    if (deliveryMode == DeliveryMode::Fixed ||
256            deliveryMode == DeliveryMode::LowestPriority) {
257        DPRINTF(LocalApic, "Interrupt is an %s.\n",
258                DeliveryMode::names[deliveryMode]);
259        // Queue up the interrupt in the IRR.
260        if (vector > IRRV)
261            IRRV = vector;
262        if (!getRegArrayBit(APIC_INTERRUPT_REQUEST_BASE, vector)) {
263            setRegArrayBit(APIC_INTERRUPT_REQUEST_BASE, vector);
264            if (level) {
265                setRegArrayBit(APIC_TRIGGER_MODE_BASE, vector);
266            } else {
267                clearRegArrayBit(APIC_TRIGGER_MODE_BASE, vector);
268            }
269        }
270    } else if (!DeliveryMode::isReserved(deliveryMode)) {
271        DPRINTF(LocalApic, "Interrupt is an %s.\n",
272                DeliveryMode::names[deliveryMode]);
273        if (deliveryMode == DeliveryMode::SMI && !pendingSmi) {
274            pendingUnmaskableInt = pendingSmi = true;
275            smiVector = vector;
276        } else if (deliveryMode == DeliveryMode::NMI && !pendingNmi) {
277            pendingUnmaskableInt = pendingNmi = true;
278            nmiVector = vector;
279        } else if (deliveryMode == DeliveryMode::ExtInt && !pendingExtInt) {
280            pendingExtInt = true;
281            extIntVector = vector;
282        } else if (deliveryMode == DeliveryMode::INIT && !pendingInit) {
283            pendingUnmaskableInt = pendingInit = true;
284            initVector = vector;
285        } else if (deliveryMode == DeliveryMode::SIPI && !pendingStartup) {
286            pendingUnmaskableInt = pendingStartup = true;
287            startupVector = vector;
288        }
289    }
290    cpu->wakeup();
291}
292
293
294void
295X86ISA::Interrupts::setCPU(BaseCPU * newCPU)
296{
297    cpu = newCPU;
298    assert(cpu);
299    regs[APIC_ID] = (cpu->cpuId() << 24);
300}
301
302
303Tick
304X86ISA::Interrupts::recvMessage(PacketPtr pkt)
305{
306    uint8_t id = (regs[APIC_ID] >> 24);
307    Addr offset = pkt->getAddr() - x86InterruptAddress(id, 0);
308    assert(pkt->cmd == MemCmd::MessageReq);
309    switch(offset)
310    {
311      case 0:
312        {
313            TriggerIntMessage message = pkt->get<TriggerIntMessage>();
314            DPRINTF(LocalApic,
315                    "Got Trigger Interrupt message with vector %#x.\n",
316                    message.vector);
317            // Make sure we're really supposed to get this.
318            assert((message.destMode == 0 && message.destination == id) ||
319                   (bits((int)message.destination, id)));
320
321            requestInterrupt(message.vector,
322                    message.deliveryMode, message.trigger);
323        }
324        break;
325      default:
326        panic("Local apic got unknown interrupt message at offset %#x.\n",
327                offset);
328        break;
329    }
330    delete pkt->req;
331    delete pkt;
332    return latency;
333}
334
335
336void
337X86ISA::Interrupts::addressRanges(AddrRangeList &range_list)
338{
339    uint8_t id = (regs[APIC_ID] >> 24);
340    range_list.clear();
341    Range<Addr> range = RangeEx(x86LocalAPICAddress(id, 0),
342                                x86LocalAPICAddress(id, 0) + PageBytes);
343    range_list.push_back(range);
344    pioAddr = range.start;
345}
346
347
348void
349X86ISA::Interrupts::getIntAddrRange(AddrRangeList &range_list)
350{
351    uint8_t id = (regs[APIC_ID] >> 24);
352    range_list.clear();
353    range_list.push_back(RangeEx(x86InterruptAddress(id, 0),
354                x86InterruptAddress(id, 0) + PhysAddrAPICRangeSize));
355}
356
357
358uint32_t
359X86ISA::Interrupts::readReg(ApicRegIndex reg)
360{
361    if (reg >= APIC_TRIGGER_MODE(0) &&
362            reg <= APIC_TRIGGER_MODE(15)) {
363        panic("Local APIC Trigger Mode registers are unimplemented.\n");
364    }
365    switch (reg) {
366      case APIC_ARBITRATION_PRIORITY:
367        panic("Local APIC Arbitration Priority register unimplemented.\n");
368        break;
369      case APIC_PROCESSOR_PRIORITY:
370        panic("Local APIC Processor Priority register unimplemented.\n");
371        break;
372      case APIC_ERROR_STATUS:
373        regs[APIC_INTERNAL_STATE] &= ~ULL(0x1);
374        break;
375      case APIC_CURRENT_COUNT:
376        {
377            if (apicTimerEvent.scheduled()) {
378                assert(clock);
379                // Compute how many m5 ticks happen per count.
380                uint64_t ticksPerCount = clock *
381                    divideFromConf(regs[APIC_DIVIDE_CONFIGURATION]);
382                // Compute how many m5 ticks are left.
383                uint64_t val = apicTimerEvent.when() - curTick;
384                // Turn that into a count.
385                val = (val + ticksPerCount - 1) / ticksPerCount;
386                return val;
387            } else {
388                return 0;
389            }
390        }
391      default:
392        break;
393    }
394    return regs[reg];
395}
396
397void
398X86ISA::Interrupts::setReg(ApicRegIndex reg, uint32_t val)
399{
400    uint32_t newVal = val;
401    if (reg >= APIC_IN_SERVICE(0) &&
402            reg <= APIC_IN_SERVICE(15)) {
403        panic("Local APIC In-Service registers are unimplemented.\n");
404    }
405    if (reg >= APIC_TRIGGER_MODE(0) &&
406            reg <= APIC_TRIGGER_MODE(15)) {
407        panic("Local APIC Trigger Mode registers are unimplemented.\n");
408    }
409    if (reg >= APIC_INTERRUPT_REQUEST(0) &&
410            reg <= APIC_INTERRUPT_REQUEST(15)) {
411        panic("Local APIC Interrupt Request registers "
412                "are unimplemented.\n");
413    }
414    switch (reg) {
415      case APIC_ID:
416        newVal = val & 0xFF;
417        break;
418      case APIC_VERSION:
419        // The Local APIC Version register is read only.
420        return;
421      case APIC_TASK_PRIORITY:
422        newVal = val & 0xFF;
423        break;
424      case APIC_ARBITRATION_PRIORITY:
425        panic("Local APIC Arbitration Priority register unimplemented.\n");
426        break;
427      case APIC_PROCESSOR_PRIORITY:
428        panic("Local APIC Processor Priority register unimplemented.\n");
429        break;
430      case APIC_EOI:
431        // Remove the interrupt that just completed from the local apic state.
432        clearRegArrayBit(APIC_IN_SERVICE_BASE, ISRV);
433        updateISRV();
434        return;
435      case APIC_LOGICAL_DESTINATION:
436        newVal = val & 0xFF000000;
437        break;
438      case APIC_DESTINATION_FORMAT:
439        newVal = val | 0x0FFFFFFF;
440        break;
441      case APIC_SPURIOUS_INTERRUPT_VECTOR:
442        regs[APIC_INTERNAL_STATE] &= ~ULL(1 << 1);
443        regs[APIC_INTERNAL_STATE] |= val & (1 << 8);
444        if (val & (1 << 9))
445            warn("Focus processor checking not implemented.\n");
446        break;
447      case APIC_ERROR_STATUS:
448        {
449            if (regs[APIC_INTERNAL_STATE] & 0x1) {
450                regs[APIC_INTERNAL_STATE] &= ~ULL(0x1);
451                newVal = 0;
452            } else {
453                regs[APIC_INTERNAL_STATE] |= ULL(0x1);
454                return;
455            }
456
457        }
458        break;
459      case APIC_INTERRUPT_COMMAND_LOW:
460        {
461            InterruptCommandRegLow low = regs[APIC_INTERRUPT_COMMAND_LOW];
462            // Check if we're already sending an IPI.
463            if (low.deliveryStatus) {
464                newVal = low;
465                break;
466            }
467            low = val;
468            InterruptCommandRegHigh high = regs[APIC_INTERRUPT_COMMAND_HIGH];
469            // Record that an IPI is being sent.
470            low.deliveryStatus = 1;
471            TriggerIntMessage message;
472            message.destination = high.destination;
473            message.vector = low.vector;
474            message.deliveryMode = low.deliveryMode;
475            message.destMode = low.destMode;
476            message.level = low.level;
477            message.trigger = low.trigger;
478            bool timing = sys->getMemoryMode() == Enums::timing;
479            switch (low.destShorthand) {
480              case 0:
481                intPort->sendMessage(message, timing);
482                break;
483              case 1:
484                panic("Self IPIs aren't implemented.\n");
485                break;
486              case 2:
487                panic("Broadcast including self IPIs aren't implemented.\n");
488                break;
489              case 3:
490                panic("Broadcast excluding self IPIs aren't implemented.\n");
491                break;
492            }
493        }
494        break;
495      case APIC_LVT_TIMER:
496      case APIC_LVT_THERMAL_SENSOR:
497      case APIC_LVT_PERFORMANCE_MONITORING_COUNTERS:
498      case APIC_LVT_LINT0:
499      case APIC_LVT_LINT1:
500      case APIC_LVT_ERROR:
501        {
502            uint64_t readOnlyMask = (1 << 12) | (1 << 14);
503            newVal = (val & ~readOnlyMask) |
504                     (regs[reg] & readOnlyMask);
505        }
506        break;
507      case APIC_INITIAL_COUNT:
508        {
509            assert(clock);
510            newVal = bits(val, 31, 0);
511            // Compute how many timer ticks we're being programmed for.
512            uint64_t newCount = newVal *
513                (divideFromConf(regs[APIC_DIVIDE_CONFIGURATION]));
514            // Schedule on the edge of the next tick plus the new count.
515            Tick offset = curTick % clock;
516            if (offset) {
517                reschedule(apicTimerEvent,
518                        curTick + (newCount + 1) * clock - offset, true);
519            } else {
520                reschedule(apicTimerEvent,
521                        curTick + newCount * clock, true);
522            }
523        }
524        break;
525      case APIC_CURRENT_COUNT:
526        //Local APIC Current Count register is read only.
527        return;
528      case APIC_DIVIDE_CONFIGURATION:
529        newVal = val & 0xB;
530        break;
531      default:
532        break;
533    }
534    regs[reg] = newVal;
535    return;
536}
537
538
539X86ISA::Interrupts::Interrupts(Params * p) :
540    BasicPioDevice(p), IntDev(this), latency(p->pio_latency), clock(0),
541    apicTimerEvent(this),
542    pendingSmi(false), smiVector(0),
543    pendingNmi(false), nmiVector(0),
544    pendingExtInt(false), extIntVector(0),
545    pendingInit(false), initVector(0),
546    pendingStartup(false), startupVector(0),
547    pendingUnmaskableInt(false)
548{
549    pioSize = PageBytes;
550    memset(regs, 0, sizeof(regs));
551    //Set the local apic DFR to the flat model.
552    regs[APIC_DESTINATION_FORMAT] = (uint32_t)(-1);
553    ISRV = 0;
554    IRRV = 0;
555}
556
557
558bool
559X86ISA::Interrupts::checkInterrupts(ThreadContext *tc) const
560{
561    RFLAGS rflags = tc->readMiscRegNoEffect(MISCREG_RFLAGS);
562    if (pendingUnmaskableInt) {
563        DPRINTF(LocalApic, "Reported pending unmaskable interrupt.\n");
564        return true;
565    }
566    if (rflags.intf) {
567        if (pendingExtInt) {
568            DPRINTF(LocalApic, "Reported pending external interrupt.\n");
569            return true;
570        }
571        if (IRRV > ISRV && bits(IRRV, 7, 4) >
572               bits(regs[APIC_TASK_PRIORITY], 7, 4)) {
573            DPRINTF(LocalApic, "Reported pending regular interrupt.\n");
574            return true;
575        }
576    }
577    return false;
578}
579
580Fault
581X86ISA::Interrupts::getInterrupt(ThreadContext *tc)
582{
583    assert(checkInterrupts(tc));
584    // These are all probably fairly uncommon, so we'll make them easier to
585    // check for.
586    if (pendingUnmaskableInt) {
587        if (pendingSmi) {
588            DPRINTF(LocalApic, "Generated SMI fault object.\n");
589            return new SystemManagementInterrupt();
590        } else if (pendingNmi) {
591            DPRINTF(LocalApic, "Generated NMI fault object.\n");
592            return new NonMaskableInterrupt(nmiVector);
593        } else if (pendingInit) {
594            DPRINTF(LocalApic, "Generated INIT fault object.\n");
595            return new InitInterrupt(initVector);
596        } else if (pendingStartup) {
597            DPRINTF(LocalApic, "Generating SIPI fault object.\n");
598            return new StartupInterrupt(startupVector);
599        } else {
600            panic("pendingUnmaskableInt set, but no unmaskable "
601                    "ints were pending.\n");
602            return NoFault;
603        }
604    } else if (pendingExtInt) {
605        DPRINTF(LocalApic, "Generated external interrupt fault object.\n");
606        return new ExternalInterrupt(extIntVector);
607    } else {
608        DPRINTF(LocalApic, "Generated regular interrupt fault object.\n");
609        // The only thing left are fixed and lowest priority interrupts.
610        return new ExternalInterrupt(IRRV);
611    }
612}
613
614void
615X86ISA::Interrupts::updateIntrInfo(ThreadContext *tc)
616{
617    assert(checkInterrupts(tc));
618    if (pendingUnmaskableInt) {
619        if (pendingSmi) {
620            DPRINTF(LocalApic, "SMI sent to core.\n");
621            pendingSmi = false;
622        } else if (pendingNmi) {
623            DPRINTF(LocalApic, "NMI sent to core.\n");
624            pendingNmi = false;
625        } else if (pendingInit) {
626            DPRINTF(LocalApic, "Init sent to core.\n");
627            pendingInit = false;
628        } else if (pendingStartup) {
629            DPRINTF(LocalApic, "SIPI sent to core.\n");
630            pendingStartup = false;
631        }
632        if (!(pendingSmi || pendingNmi || pendingInit || pendingStartup))
633            pendingUnmaskableInt = false;
634    } else if (pendingExtInt) {
635        pendingExtInt = false;
636    } else {
637        DPRINTF(LocalApic, "Interrupt %d sent to core.\n", IRRV);
638        // Mark the interrupt as "in service".
639        ISRV = IRRV;
640        setRegArrayBit(APIC_IN_SERVICE_BASE, ISRV);
641        // Clear it out of the IRR.
642        clearRegArrayBit(APIC_INTERRUPT_REQUEST_BASE, IRRV);
643        updateIRRV();
644    }
645}
646
647X86ISA::Interrupts *
648X86LocalApicParams::create()
649{
650    return new X86ISA::Interrupts(this);
651}
652